有没有办法通知 C# 编译器我不想等待异步调用?



给定如下代码(用于测试(:

tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
while (!cancellation.IsCancellationRequested)
{
var client = await tcpListener.AcceptTcpClientAsync();
//Monitor services TCP messages to this client
var monitor = new Monitor(client);
monitor.MonitorAsync(cancellation.Token);
}

我明确不想awaitMonitorAsync,因为我希望每个监视器并行运行,但是Visual Studio警告我(当然是正确的(,代码执行将继续,而无需等待MonitorAsync完成。

我宁愿避免编译器警告,并将它们视为我做错/不明智事情的标志。那么,是否有一种"适当"的方法来做到这一点,以满足编译器的有意行为?

只需分配任务并使用丢弃运算符忽略它:

_ = monitor.MonitorAsync(cancellation.Token);

最新更新