C# 在UI线程上同步取消挂起的任务
有时,一旦我使用请求取消挂起的任务,我需要确保任务已正确达到取消状态,然后才能继续。大多数情况下,当应用程序终止时,我会遇到这种情况,我想优雅地取消所有挂起的任务。然而,这也可能是UI工作流规范的一项要求,当新的后台流程只有在当前挂起的流程完全取消或自然结束时才能启动 如果有人能分享他/她的处理这种情况的方法,我将不胜感激。我说的是以下模式:C# 在UI线程上同步取消挂起的任务,c#,.net,multithreading,task-parallel-library,async-await,C#,.net,Multithreading,Task Parallel Library,Async Await,有时,一旦我使用请求取消挂起的任务,我需要确保任务已正确达到取消状态,然后才能继续。大多数情况下,当应用程序终止时,我会遇到这种情况,我想优雅地取消所有挂起的任务。然而,这也可能是UI工作流规范的一项要求,当新的后台流程只有在当前挂起的流程完全取消或自然结束时才能启动 如果有人能分享他/她的处理这种情况的方法,我将不胜感激。我说的是以下模式: _cancellationTokenSource.Cancel(); _task.Wait(); 正如所知,当在UI线程上使用时,它很容易导致死锁。但是
_cancellationTokenSource.Cancel();
_task.Wait();
正如所知,当在UI线程上使用时,它很容易导致死锁。但是,并非总是可以使用异步等待(即等待任务
;例如,这是可能的情况之一)。同时,简单地请求取消并继续而不实际观察其状态是一种代码味道
作为说明问题的简单示例,我可能希望确保在FormClosing
事件处理程序中已完全取消以下doworksync
任务。如果我不等待<代码>任务> <代码>代码>主窗体关闭>代码>,我可能甚至看不到当前工作项的<代码>“完成工作项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线程的消息循环必须继续泵送消息,因此doworksync
(在线程的windowsformsssynchronizationcontext
)中的异步继续有可能被执行,并最终达到取消状态。但是,泵被\u task.Wait()
阻塞,从而导致死锁。此示例特定于WinForms,但问题也与WPF上下文相关
在这种情况下,我看不到任何其他解决方案,只能组织一个嵌套的消息循环,而在远处等待\u任务
,这类似于,它在等待线程终止时不断抽取消息。该框架似乎没有为此提供明确的任务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;
}
}
}
使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间WinformsApp
{
公共部分类主窗体:窗体
{
取消令牌源;
任务-任务;
//表单加载事件
void main form_加载(对象发送方,事件参数e)
{
_cts=新的CancellationTokenSource();
_task=doworksync(_cts.Token);
}
//表格结束活动
void main form_FormClosing(对象发送方,FormClosingEventArgs e)
{
//禁用用户界面
var wasaenabled=this.Enabled;this.Enabled=false;
尝试
{
//请求取消
_cts.Cancel();
//正在发送消息时等待
_task.AsWaitHandle().WaitWithDoEvents();
}
捕获(例外情况除外)
{
如果(ex是AggregateException)
ex=ex.InnerException;
如果(!(ex是OperationCanceledException))
投掷;
}
最后
{
//启用UI
this.Enabled=已启用;
}
MessageBox.Show(“任务已取消”);
}
//异步工作
异步任务DoWorkAsync(取消令牌ct)
{
var i=0;
while(true)
{
ct.ThrowIfCancellationRequested();
变量项=i++;
等待任务。运行(()=>
{
调试.打印(“开始工作项”+项);
//使用睡眠模拟某些无法取消的原子操作
睡眠(1000);
调试.打印(“已完成工作项”+项);
},ct);
}
}
公共表格(
{
初始化组件();
this.FormClosing+=MainForm\u FormClosing;
此.Load+=MainForm_Load;
}
}
///
///WaitHandle和任务扩展
///按比例—https://stackoverflow.com/users/1768303/noseratio
///
公共静态类WaitExt
{
///
///等待一个手柄,并使用DoEvents泵送消息
///
公共静态bool WaitWithDoEvents(此WaitHandle句柄、CancellationToken令牌、int超时)
{
if(SynchronizationContext.Current作为System.Windows.Forms.WindowsFormsSynchronizationContext==null)
{
// https://stackoverflow.com/a/19555959
抛出新的ApplicationException(“内部错误:必须在具有WindowsFormsSynchronizationContext的线程上调用WaitWithDoEvents。”);
}
consuint EVENT_MASK=Win32.QS_ALLINPUT;
IntPtr[]handles={handle.SafeWaitHandle.DangerousGetHandle()};
//如果不是无限长,则跟踪超时
Func hastinmedout=()=>假;
整数剩余超时=超时;
if(timeout!=timeout.Infinite)
{
int startTick=Environment.TickCount;
黑斯米德=()=>
{
//即使连续运行,Environment.TickCount也能正确包装
int-lapse=Environment.TickCount-startTick;
remainingTimeout=Math.Max(timeout-la
public delegate void AsyncMethodCaller(CancellationToken ct);
private CancellationTokenSource _cts;
private AsyncMethodCaller caller;
private IAsyncResult methodResult;
// Form Load event
private void MainForm_Load(object sender, EventArgs e)
{
_cts = new CancellationTokenSource();
caller = new AsyncMethodCaller(DoWorkAsync);
methodResult = caller.BeginInvoke(_cts.Token,
ar =>
{
},
null);
}
// Form Closing event
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_cts.Cancel();
MessageBox.Show("Task cancellation requested");
}
// async work
private void DoWorkAsync(CancellationToken ct)
{
var i = 0;
while (true)
{
var item = i++;
Debug.Print("Starting work item " + item);
// use Sleep as a mock for some atomic operation which cannot be cancelled
Thread.Sleep(10000);
Debug.Print("Finished work item " + item);
if (ct.IsCancellationRequested)
{
return;
}
}
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
methodResult.AsyncWaitHandle.WaitOne();
MessageBox.Show("Task cancelled");
}
Debug.Print("Starting work item " + item);
// use Sleep as a mock for some atomic operation which cannot be cancelled
Thread.Sleep(10000);
ct.ThrowIfCancellationRequested();
Debug.Print("Finished work item " + item);
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_task.IsCompleted)
{
e.Cancel = true;
_cts.Cancel();
_task.ContinueWith(t => Close(),
TaskScheduler.FromCurrentSynchronizationContext());
}
}
using System;
using System.Diagnostics;
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)
{
ShowModalWaitMessage();
}
// Show a message and wait
void ShowModalWaitMessage()
{
var dialog = new Form();
dialog.Load += async (s, e) =>
{
_cts.Cancel();
try
{
// show the dialog for at least 2 secs
await Task.WhenAll(_task, Task.Delay(2000));
}
catch (Exception ex)
{
while (ex is AggregateException)
ex = ex.InnerException;
if (!(ex is OperationCanceledException))
throw;
}
dialog.Close();
};
dialog.ShowIcon = false; dialog.ShowInTaskbar = false;
dialog.FormBorderStyle = FormBorderStyle.FixedToolWindow;
dialog.StartPosition = FormStartPosition.CenterParent;
dialog.Width = 160; dialog.Height = 100;
var label = new Label();
label.Text = "Closing, please wait...";
label.AutoSize = true;
dialog.Controls.Add(label);
dialog.ShowDialog();
}
// 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;
}
}
}