【发布时间】:2015-06-03 17:21:33
【问题描述】:
以下代码按我的意愿运行,但会导致警告:
Warning 1 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
是否有替代Task.Run() 的方法可以以简洁的方式启动此线程?
/// <summary>
/// StartSubscriptionsAsync must be called if you want subscription change notifications.
/// This starts the subscription engine. We always create one subscription for
/// Home DisplayName to start (but ignore any updates).
/// </summary>
public async Task StartSubscriptionsAsync(){
await _subscriptionClient.ConnectAsync(Host, Port);
// Generates a compiler warning, but it is what we want
Task.Run(() => ReadSubscriptionResponses());
// We do a GetValue so we know we have a good connection
SendRequest("sys://Home?f??" + "Name");
if (FastMode) EnableFastMode();
foreach (var subscription in _subscriptions) {
SendSubscriptionRequest(subscription.Value);
}
}
【问题讨论】:
-
您可以随时通过将代码块包装在
#pragma warning disable/enable中来禁用此处的警告(不过,我现在不记得警告代码了,抱歉)... -
看这里msdn.microsoft.com/en-us/library/vstudio/hh873131.aspx这种情况建议你取消警告。
标签: c# async-await tcpclient