gpt4 book ai didi

c# - 在 UI 线程上同步取消挂起的任务

转载 作者:IT王子 更新时间:2023-10-29 04:30:53 24 4
gpt4 key购买 nike

有时,一旦我用 CancellationTokenSource.Cancel 请求取消一个挂起的任务,我需要确保任务已正确达到取消状态,然后才能继续。当应用程序终止并且我想优雅地取消所有未决任务时,我经常遇到这种情况。然而,这也可能是 UI 工作流规范的要求,当新的后台进程只有在当前挂起的进程已完全取消或自然结束时才能启动。

如果有人分享他/她处理这种情况的方法,我将不胜感激。我说的是以下模式:

_cancellationTokenSource.Cancel();
_task.Wait();

众所周知,在 UI 线程上使用时很容易导致死锁。但是,并不总是可以使用异步等待来代替(即 await task;例如,here可能的情况之一)。同时,简单地请求取消并继续而不实际观察其状态是一种代码味道。

作为说明问题的简单示例,我可能想确保以下 DoWorkAsync 任务已在 FormClosing 事件处理程序中完全取消。如果我不等待 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 应用来说应该很常见,但我发现关于这个主题的资料很少。 理想情况下,后台任务进程应该设计成不需要消息泵来支持同步取消,但我认为这并不总是可能的。

我错过了什么吗?是否有其他可能更便携的方式/模式来处理它?<​​/p>

最佳答案

所以我们不希望进行同步等待,因为这会阻塞 UI 线程,并且还可能导致死锁。

异步处理的问题很简单,表单将在您“准备好”之前关闭。这是可以解决的;如果异步任务尚未完成,只需取消关闭表单,然后在任务确实完成时再次“真正”关闭它。

该方法看起来像这样(省略了错误处理):

void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_task.IsCompleted)
{
e.Cancel = true;
_cts.Cancel();
_task.ContinueWith(t => Close(),
TaskScheduler.FromCurrentSynchronizationContext());
}
}

请注意,为了使错误处理更容易,您此时也可以使方法async,而不是使用显式延续。

关于c# - 在 UI 线程上同步取消挂起的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com