本文介绍了在UI线程同步取消挂起任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,当我要求挂起任务的取消用的,我需要确保的的任务已正确地达到取消状态,这样我才可以继续。大多数时候我面对,当应用程序被终止这种情况,我想平稳地取消所有未决的任务。但是,它也可以是在UI工作流程规范中,要求当新的背景过程只能启动如果当前未决一个已经被完全取消或自然达到其端

Sometimes, once I have requested the cancellation of a pending task with CancellationTokenSource.Cancel, I need to make sure the task has properly reached the cancelled state, before I can continue. Most often I face this situation when the app is terminating and I want to cancel all pending task gracefully. However, it can also be a requirement of the UI workflow specification, when the new background process can only start if the current pending one has been fully cancelled or reached its end naturally.

我倒是AP preciate如果有人在共享处理这种情况,他/她的做法我说的是以下模式:

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

由于是,已知为能够容易产生死锁UI线程上使用时。然而,它并不总是可以使用一个异步等待,而不是(即等待任务;如的是案例之一时,它的的可能)。同时,它是一个code气味来简单地要求取消和继续,而无需实际观察它的状态。

As is, it is known to be capable of easily causing a deadlock when used on the UI thread. However, it is not always possible to use an asynchronous wait instead (i.e. await task; e.g., here is one of the cases when it is possible). At the same time, it is a code smell to simply request the cancellation and continue without actually observing its state.

举一个简单的例子,说明这个问题,我可能希望确保以下 DoWorkAsync 任务内的FormClosing 事件处理程序。如果我没有等到 _Task MainForm_FormClosing ,我可能连见 完成的工作项目N跟踪当前工作项目,如应用程序终止挂起的子任务执行过程中(这是在一个池线程执行)。如果我等到的是,它导致死锁:

As a simple example illustrating the problem, I may want to make sure the following DoWorkAsync task has been fully cancelled inside FormClosing event handler. If I don't wait for the _task inside MainForm_FormClosing, I may not even see the "Finished work item N" trace for the current work item, as the app terminates in the middle of a pending sub-task (which is executed on a pool thread). If I do wait though, it results in a deadlock:

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的上下文中,也

That happens because the UI thread's message loop has to continue pumping messages, so the asynchronous continuation inside DoWorkAsync (which is scheduled on the thread's WindowsFormsSynchronizationContext) has a chance to be executed and eventually have reached the cancelled state. However, the pump is blocked with _task.Wait(), which leads to the deadlock. This example is specific to WinForms, but the problem is relevant in the context of WPF, too.

在这种情况下,我没有看到任何其他的解决方案,但组织一个嵌套的消息循环,同时等待 _Task 在一个遥远的距离,它类似于的,这使抽水的消息在等待线程终止。该框架似乎并没有为此提供一个明确的任务的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 - http://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)
            {
                // http://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;
        }
    }
}

我相信所描述的场景应该是常见的用户界面应用程序pretty的,但我发现很少的材料,在这个问题上。 理想的情况下,后台任务的过程应该设计在不需要消息泵来支持同步取消的方式,但我不认为这是总是可能的。

I believe the described scenario ought to be pretty common for the UI apps, yet I have found very little material on this subject. Ideally, the background task process should be designed in the way it doesn't require a message pump to support synchronous cancellation, but I don't think this is always possible.

我缺少的东西?还有没有其他的,也许更便携的方式/模式来应对呢?

Am I missing something? Are there other, perhaps more portable ways/patterns to deal with it?

推荐答案

因此​​,我们不希望做一个同步的等待,因为这将被阻塞UI线程,也可能是死锁。

So we don't want to be doing a synchronous wait as that would be blocking the UI thread, and also possibly deadlocking.

取放异步的问题很简单,你是准备好之前的形式将关闭。可固定的;干脆取消了形式结束,如果异步任务还没有完成,然后再次关闭它真正的当任务的确实的结束。

The problem with handling it asynchronously is simply that the form will be closed before you're "ready". That can be fixed; simply cancel the form closing if the asynchronous task isn't done yet, and then close it again "for real" when the task does finish.

该方法可以是这个样子(处理忽略错误):

The method can look something like this (error handling omitted):

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

需要注意的是,使错误处理更容易,你可以在这一点上做的,而不是使用显式的延续方法异步为好,。

这篇关于在UI线程同步取消挂起任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:55