1 回答
TA贡献1801条经验 获得超16个赞
async void应该转换为 anasync Task以及您应该等待Task.Delay而不是调用Thread.Sleep. 如果要异步,则需要一直异步
static async Task Receive(ITdlMessageReceiver receiver, ILogger logger) {
while (true) {
var message = await receiver.ReceiveAsync<TdlMessage<object>>(topic, entity);
if (message != null) {
logger.LogDebug($"Message received. Topic: {topic}. Action: {Enum.GetName(typeof(TopicActions), message.Action)}. Message: {JsonConvert.SerializeObject(message)}.");
}
await Task.Delay(sleepTime);
}
}
尝试使代码一直异步,是的,但是作为控制台应用程序(单线程),您将被允许调用Wait()该Receive方法,Main因为它不会混合调用会导致异步流出现问题。
public static void Main(string[] args) {
//...
//...
//...
Receive(receiver, logger).Wait();
}
- 1 回答
- 0 关注
- 154 浏览
添加回答
举报