任务不会被垃圾收集

时间:2019-08-23 12:15:06

标签: c# .net multithreading winforms async-await

这不是重复的 Task not garbage collected。虽然症状相似。

下面的代码是一个控制台应用程序,可创建用于WinForms的STA线程。通过使用TaskScheduler.FromCurrentSynchronizationContext获得的自定义任务计划程序将任务发布到该线程,该任务计划程序仅在此处隐式包装WindowsFormsSynchronizationContext的实例。

根据导致此STA线程结束的原因,在var terminatorTask = Run(() => Application.ExitThread())方法中安排的最终任务WinformsApartment.Dispose不一定总是有执行的机会。不管怎样,我相信该任务仍应进行垃圾收集,但不是。为什么?

下面是一个独立的示例,该示例说明了{s_debugTaskRef.IsAlive在最后是true,并通过.NET 4.8(调试和发行版)进行了测试:

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleTest
{
    class Program
    {
        // entry point
        static async Task Main(string[] args)
        {
            try
            {
                using (var apartment = new WinformsApartment(() => new Form()))
                {
                    await Task.Delay(1000);
                    await apartment.Run(() => Application.ExitThread());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
                Environment.Exit(-1);
            }

            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            Console.WriteLine($"IsAlive: {WinformsApartment.s_debugTaskRef.IsAlive}");
            Console.ReadLine();
        }
    }

    public class WinformsApartment : IDisposable
    {
        readonly Thread _thread; // the STA thread

        readonly TaskScheduler _taskScheduler; // the STA thread's task scheduler

        readonly Task _threadEndTask; // to keep track of the STA thread completion

        readonly object _lock = new object();

        public TaskScheduler TaskScheduler { get { return _taskScheduler; } }

        public Task AsTask { get { return _threadEndTask; } }

        /// <summary>MessageLoopApartment constructor</summary>
        public WinformsApartment(Func<Form> createForm)
        {
            var schedulerTcs = new TaskCompletionSource<TaskScheduler>();

            var threadEndTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            // start an STA thread and gets a task scheduler
            _thread = new Thread(_ =>
            {
                try
                {
                    // handle Application.Idle just once
                    // to make sure we're inside the message loop
                    // and the proper synchronization context has been correctly installed

                    void onIdle(object s, EventArgs e) {
                        Application.Idle -= onIdle;
                        // make the task scheduler available
                        schedulerTcs.SetResult(TaskScheduler.FromCurrentSynchronizationContext());
                    };

                    Application.Idle += onIdle;
                    Application.Run(createForm());

                    threadEndTcs.TrySetResult(true);
                }
                catch (Exception ex)
                {
                    threadEndTcs.TrySetException(ex);
                }
            });

            async Task waitForThreadEndAsync()
            {
                // we use TaskCreationOptions.RunContinuationsAsynchronously
                // to make sure thread.Join() won't try to join itself
                Debug.Assert(Thread.CurrentThread != _thread);
                await threadEndTcs.Task.ConfigureAwait(false);
                _thread.Join();
            }

            _thread.SetApartmentState(ApartmentState.STA);
            _thread.IsBackground = true;
            _thread.Start();

            _taskScheduler = schedulerTcs.Task.Result;
            _threadEndTask = waitForThreadEndAsync();
        }

        // TODO: it's here for debugging leaks
        public static readonly WeakReference s_debugTaskRef = new WeakReference(null); 

        /// <summary>shutdown the STA thread</summary>
        public void Dispose()
        {
            lock(_lock)
            {
                if (Thread.CurrentThread == _thread)
                    throw new InvalidOperationException();

                if (!_threadEndTask.IsCompleted)
                {
                    // execute Application.ExitThread() on the STA thread
                    var terminatorTask = Run(() => Application.ExitThread());

                    s_debugTaskRef.Target = terminatorTask; // TODO: it's here for debugging leaks

                    _threadEndTask.GetAwaiter().GetResult();
                }
            }
        }

        /// <summary>Task.Factory.StartNew wrappers</summary>
        public Task Run(Action action, CancellationToken token = default(CancellationToken))
        {
            return Task.Factory.StartNew(action, token, TaskCreationOptions.None, _taskScheduler);
        }

        public Task<TResult> Run<TResult>(Func<TResult> action, CancellationToken token = default(CancellationToken))
        {
            return Task.Factory.StartNew(action, token, TaskCreationOptions.None, _taskScheduler);
        }

        public Task Run(Func<Task> action, CancellationToken token = default(CancellationToken))
        {
            return Task.Factory.StartNew(action, token, TaskCreationOptions.None, _taskScheduler).Unwrap();
        }

        public Task<TResult> Run<TResult>(Func<Task<TResult>> action, CancellationToken token = default(CancellationToken))
        {
            return Task.Factory.StartNew(action, token, TaskCreationOptions.None, _taskScheduler).Unwrap();
        }
    }
}

我怀疑这可能是.NET Framework错误。我目前正在调查中,我会发布可能发现的内容,但是也许有人可以立即提供解释。

1 个答案:

答案 0 :(得分:2)

好吧,看来$('#SummaryTableForm').attr("class","dirty");在这里没有得到正确处理。不确定是错误还是“功能”,但以下更改确实可以解决问题:

WindowsFormsSynchronizationContext

现在SynchronizationContext syncContext = null; void onIdle(object s, EventArgs e) { Application.Idle -= onIdle; syncContext = SynchronizationContext.Current; // make the task scheduler available schedulerTcs.SetResult(TaskScheduler.FromCurrentSynchronizationContext()); }; Application.Idle += onIdle; Application.Run(createForm()); SynchronizationContext.SetSynchronizationContext(null); (syncContext as IDisposable)?.Dispose(); IsAlive,任务已正确进行GC处理。注释掉上面的false,然后(syncContext as IDisposable)?.Dispose()回到IsAlive

已更新,如果有人使用类似的模式(我本人将其用于自动化),那么我建议您明确控制true的生存期和处置方式:

WindowsFormsSynchronizationContext