【发布时间】:2014-01-19 12:41:37
【问题描述】:
有时,一旦我使用CancellationTokenSource.Cancel 请求取消挂起的任务,我需要确保任务已正确达到取消状态,然后才能继续。大多数情况下,当应用程序终止并且我想优雅地取消所有待处理的任务时,我会遇到这种情况。但是,这也可能是 UI 工作流规范的要求,即只有在当前待处理的进程完全取消或自然结束时才能启动新的后台进程。
如果有人分享他/她处理这种情况的方法,我将不胜感激。我说的是以下模式:
_cancellationTokenSource.Cancel();
_task.Wait();
众所周知,当在 UI 线程上使用时,它很容易导致死锁。但是,并不总是可以使用异步等待来代替(即await task;例如,here 是可能的情况之一)。同时,简单地请求取消并继续而不实际观察其状态是一种代码味道。
作为一个说明问题的简单示例,我可能想确保在FormClosing 事件处理程序中完全取消了以下DoWorkAsync 任务。如果我不等待MainForm_FormClosing 中的_task,我什至可能看不到当前工作项的"Finished work item N" 跟踪,因为应用程序在待处理的子任务(在一个池线程)。但是,如果我确实等待,则会导致死锁:
public partial class MainForm : Form
{
CancellationTokenSource _cts;
Task _task;
// Form Load event
void MainForm_Load(object sender, EventArgs e)
{
_cts = new CancellationTokenSource();
_task = DoWorkAsync(_cts.Token);
}
// Form Closing event
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_cts.Cancel();
try
{
// if we don't wait here,
// we may not see "Finished work item N" for the current item,
// if we do wait, we'll have a deadlock
_task.Wait();
}
catch (Exception ex)
{
if (ex is AggregateException)
ex = ex.InnerException;
if (!(ex is OperationCanceledException))
throw;
}
MessageBox.Show("Task cancelled");
}
// async work
async Task DoWorkAsync(CancellationToken ct)
{
var i = 0;
while (true)
{
ct.ThrowIfCancellationRequested();
var item = i++;
await Task.Run(() =>
{
Debug.Print("Starting work item " + item);
// use Sleep as a mock for some atomic operation which cannot be cancelled
Thread.Sleep(1000);
Debug.Print("Finished work item " + item);
}, ct);
}
}
}
发生这种情况是因为 UI 线程的消息循环必须继续泵送消息,因此 DoWorkAsync 内部的异步延续(调度在线程的 WindowsFormsSynchronizationContext 上)有机会执行并最终达到取消状态。但是,泵被_task.Wait() 阻塞,导致死锁。此示例特定于 WinForms,但该问题也与 WPF 的上下文相关。
在这种情况下,我没有看到任何其他解决方案,除了组织一个嵌套的消息循环,同时等待_task。在遥远的方式,它类似于Thread.Join ,它在等待线程终止时不断发送消息。该框架似乎没有为此提供明确的任务 API,因此我最终提出了以下WaitWithDoEvents 的实现:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinformsApp
{
public partial class MainForm : Form
{
CancellationTokenSource _cts;
Task _task;
// Form Load event
void MainForm_Load(object sender, EventArgs e)
{
_cts = new CancellationTokenSource();
_task = DoWorkAsync(_cts.Token);
}
// Form Closing event
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// disable the UI
var wasEnabled = this.Enabled; this.Enabled = false;
try
{
// request cancellation
_cts.Cancel();
// wait while pumping messages
_task.AsWaitHandle().WaitWithDoEvents();
}
catch (Exception ex)
{
if (ex is AggregateException)
ex = ex.InnerException;
if (!(ex is OperationCanceledException))
throw;
}
finally
{
// enable the UI
this.Enabled = wasEnabled;
}
MessageBox.Show("Task cancelled");
}
// async work
async Task DoWorkAsync(CancellationToken ct)
{
var i = 0;
while (true)
{
ct.ThrowIfCancellationRequested();
var item = i++;
await Task.Run(() =>
{
Debug.Print("Starting work item " + item);
// use Sleep as a mock for some atomic operation which cannot be cancelled
Thread.Sleep(1000);
Debug.Print("Finished work item " + item);
}, ct);
}
}
public MainForm()
{
InitializeComponent();
this.FormClosing += MainForm_FormClosing;
this.Load += MainForm_Load;
}
}
/// <summary>
/// WaitHandle and Task extensions
/// by Noseratio - https://stackoverflow.com/users/1768303/noseratio
/// </summary>
public static class WaitExt
{
/// <summary>
/// Wait for a handle and pump messages with DoEvents
/// </summary>
public static bool WaitWithDoEvents(this WaitHandle handle, CancellationToken token, int timeout)
{
if (SynchronizationContext.Current as System.Windows.Forms.WindowsFormsSynchronizationContext == null)
{
// https://stackoverflow.com/a/19555959
throw new ApplicationException("Internal error: WaitWithDoEvents must be called on a thread with WindowsFormsSynchronizationContext.");
}
const uint EVENT_MASK = Win32.QS_ALLINPUT;
IntPtr[] handles = { handle.SafeWaitHandle.DangerousGetHandle() };
// track timeout if not infinite
Func<bool> hasTimedOut = () => false;
int remainingTimeout = timeout;
if (timeout != Timeout.Infinite)
{
int startTick = Environment.TickCount;
hasTimedOut = () =>
{
// Environment.TickCount wraps correctly even if runs continuously
int lapse = Environment.TickCount - startTick;
remainingTimeout = Math.Max(timeout - lapse, 0);
return remainingTimeout <= 0;
};
}
// pump messages
while (true)
{
// throw if cancellation requested from outside
token.ThrowIfCancellationRequested();
// do an instant check
if (handle.WaitOne(0))
return true;
// pump the pending message
System.Windows.Forms.Application.DoEvents();
// check if timed out
if (hasTimedOut())
return false;
// the queue status high word is non-zero if a Windows message is still in the queue
if ((Win32.GetQueueStatus(EVENT_MASK) >> 16) != 0)
continue;
// the message queue is empty, raise Idle event
System.Windows.Forms.Application.RaiseIdle(EventArgs.Empty);
if (hasTimedOut())
return false;
// wait for either a Windows message or the handle
// MWMO_INPUTAVAILABLE also observes messages already seen (e.g. with PeekMessage) but not removed from the queue
var result = Win32.MsgWaitForMultipleObjectsEx(1, handles, (uint)remainingTimeout, EVENT_MASK, Win32.MWMO_INPUTAVAILABLE);
if (result == Win32.WAIT_OBJECT_0 || result == Win32.WAIT_ABANDONED_0)
return true; // handle signalled
if (result == Win32.WAIT_TIMEOUT)
return false; // timed out
if (result == Win32.WAIT_OBJECT_0 + 1) // an input/message pending
continue;
// unexpected result
throw new InvalidOperationException();
}
}
public static bool WaitWithDoEvents(this WaitHandle handle, int timeout)
{
return WaitWithDoEvents(handle, CancellationToken.None, timeout);
}
public static bool WaitWithDoEvents(this WaitHandle handle)
{
return WaitWithDoEvents(handle, CancellationToken.None, Timeout.Infinite);
}
public static WaitHandle AsWaitHandle(this Task task)
{
return ((IAsyncResult)task).AsyncWaitHandle;
}
/// <summary>
/// Win32 interop declarations
/// </summary>
public static class Win32
{
[DllImport("user32.dll")]
public static extern uint GetQueueStatus(uint flags);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint MsgWaitForMultipleObjectsEx(
uint nCount, IntPtr[] pHandles, uint dwMilliseconds, uint dwWakeMask, uint dwFlags);
public const uint QS_KEY = 0x0001;
public const uint QS_MOUSEMOVE = 0x0002;
public const uint QS_MOUSEBUTTON = 0x0004;
public const uint QS_POSTMESSAGE = 0x0008;
public const uint QS_TIMER = 0x0010;
public const uint QS_PAINT = 0x0020;
public const uint QS_SENDMESSAGE = 0x0040;
public const uint QS_HOTKEY = 0x0080;
public const uint QS_ALLPOSTMESSAGE = 0x0100;
public const uint QS_RAWINPUT = 0x0400;
public const uint QS_MOUSE = (QS_MOUSEMOVE | QS_MOUSEBUTTON);
public const uint QS_INPUT = (QS_MOUSE | QS_KEY | QS_RAWINPUT);
public const uint QS_ALLEVENTS = (QS_INPUT | QS_POSTMESSAGE | QS_TIMER | QS_PAINT | QS_HOTKEY);
public const uint QS_ALLINPUT = (QS_INPUT | QS_POSTMESSAGE | QS_TIMER | QS_PAINT | QS_HOTKEY | QS_SENDMESSAGE);
public const uint MWMO_INPUTAVAILABLE = 0x0004;
public const uint WAIT_TIMEOUT = 0x00000102;
public const uint WAIT_FAILED = 0xFFFFFFFF;
public const uint INFINITE = 0xFFFFFFFF;
public const uint WAIT_OBJECT_0 = 0;
public const uint WAIT_ABANDONED_0 = 0x00000080;
}
}
}
我相信所描述的场景对于 UI 应用程序来说应该很常见,但我发现关于这个主题的材料很少。 理想情况下,后台任务进程应该设计成不需要消息泵来支持同步取消,但我认为这并不总是可能的。
我错过了什么吗?还有其他可能更便携的方式/模式来处理它吗?
【问题讨论】:
-
作为一个快速说明,您不应该将 cts 传递到您的等待呼叫中吗?即使用 await Task.Delay(_cts) 或类似的东西而不是 Thread.Sleep
-
@LukeMcGregor,我故意将
Sleep放在那里,就像Task.Run启动的一些原子CPU 绑定操作的模拟,它不支持即时取消。事实上,如果我在这个示例代码中使用await Task.Delay(1000, ct)而不是await Task.Run(...),它不会导致死锁。但是,以下仍然可以:await Task.Run(async () => { /* do some pool thread work first, then delay */ await Task.Delay(1000, ct); }); -
使用 Wait 阻塞 UI 方法是一种反模式。为什么不在令牌上注册回调,并在异步任务完成并处理您的结果时让它返回给您。 msdn.microsoft.com/en-us/library/dd321663%28v=vs.110%29.aspx
-
@Random,
ct.Register(callback, useSynchronizationContext: false)在取消后台 IO 任务时确实很有帮助。我确实使用它,例如通过从callback调用HttpClient.CancelPendingRequests来取消挂起的HTTP 请求。但是,我看不出它如何应用于像上面的DoWorkAsync这样的情况,在这种情况下,CPU 绑定的后台工作块是从 UI 线程开始的。如果您可以详细说明,请随时发布答案。 -
好的,我将睡眠增加到了更高的数字,我明白你的意思...
标签: c# .net multithreading task-parallel-library async-await