我有以下异步操作。操作完成后,将调用dataSentHandler
函数。我想知道是否可以在一段时间后取消操作。
DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
dswo.Completed = new AsyncOperationCompletedHandler<uint>(DataSentHandler);
答案 0 :(得分:0)
根据文档,您可以使用networkDataWriter.Cancel()
如果请求成功,您可以使用计时器检查并启动超时并取消计时器。
DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
dswo.Completed = new AsyncOperationCompletedHandler<uint>(DataSentHandler);
Timer timer = new Timer(1000);
timer.Elapsed += () => { networkDataWriter.Cancel(); };
timer.Start();
来源: https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.datawriterstoreoperation
https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx