【发布时间】:2016-02-20 21:02:04
【问题描述】:
我的应用程序中发生了一些奇怪的事情,我无法理解。我使用 ThreadPool 类为网络通信创建了一个后台任务。在这个任务中,我异步调用了一些方法。原因是有时微软没有提供同步方法(例如没有方法DatagramSocket.Connect所以我必须使用方法DatagramSocket.ConnectAsync)。但是因为我需要同步调用这些方法,所以我必须使用关键字“await”并将方法标记为“async”。当我这样做时,在 Button_Click 事件处理程序启动中创建的后台任务的事件处理程序被过早地调用。当后台任务真正完成其工作时(例如,通过手动将执行指针移动到 UdpSend 方法的末尾来中断调试器中的执行),不会调用 OnCompleted 事件处理程序。这是正常的吗?我错过了什么重要的东西吗?
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
IAsyncAction work;
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (work == null)
{
ButtonStart.Content = "Stop UDP test";
work = ThreadPool.RunAsync(UdpSend);
work.Completed = OnUdpSendFinish;
}
else
{
work.Cancel();
}
}
private async void UdpSend(IAsyncAction work)
{
DatagramSocket socket = new DatagramSocket();
socket.MessageReceived += Socket_MessageReceived;
HostName host_name = new HostName("10.0.0.2");
await socket.ConnectAsync(host_name, "1234");
DataWriter writer = new DataWriter(socket.OutputStream);
uint data = 0;
int payload_size = 512;
int payload_len = payload_size / sizeof(uint);
while (work.Status != AsyncStatus.Canceled)
{
for (int i = 0; i < payload_len; i++)
{
writer.WriteUInt32(data++);
}
await writer.StoreAsync();
}
}
private void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
throw new System.NotImplementedException();
}
private async void OnUdpSendFinish(IAsyncAction asyncInfo, AsyncStatus asyncStatus)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
ButtonStart.Content = "Start UDP test";
});
work = null;
}
}
【问题讨论】:
-
关键是
await不允许让你同步调用它们。它们仍然是异步的。
标签: c# asynchronous visual-studio-2015 win-universal-app