寻找一个自定义的SynchronizationContext的例子(用于单元测试) [英] Looking for an example of a custom SynchronizationContext (Required for unit testing)

查看:221
本文介绍了寻找一个自定义的SynchronizationContext的例子(用于单元测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个自定义的<一个href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx">SynchronizationContext即:

I need a custom SynchronizationContext that:

  • 旗下拥有运行上岗和一个单独的线程发送代表
  • 是否发送在它们发送的顺序
  • 在不需要任何其他的方法

我需要这个,所以我可以单元测试一些线程code,它会跟WinForm的在实际应用。

I need this so I can unit test some threading code that will talk to WinForm in the real application.

在我写我自己,我希望有人可以点我到一个简单的(小)的实现。

Before I write my own, I was hoping that someone could point me to a simple (and small) implementations.

推荐答案

这本是前一段时间我写的,有版权的任何问题,也无法保证(系统没有投入生产):

This one was written by me some time ago, no issues with copyright, no guarantees either(the system didn't go into production):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;

namespace ManagedHelpers.Threads
{
    public class STASynchronizationContext : SynchronizationContext, IDisposable
    {
        private readonly Dispatcher dispatcher;
        private object dispObj;
        private readonly Thread mainThread;

        public STASynchronizationContext()
        {
            mainThread = new Thread(MainThread) { Name = "STASynchronizationContextMainThread", IsBackground = false };
            mainThread.SetApartmentState(ApartmentState.STA);
            mainThread.Start();

            //wait to get the main thread's dispatcher
            while (Thread.VolatileRead(ref dispObj) == null)
                Thread.Yield();

            dispatcher = dispObj as Dispatcher;
        }

        public override void Post(SendOrPostCallback d, object state)
        {
            dispatcher.BeginInvoke(d, new object[] { state });
        }

        public override void Send(SendOrPostCallback d, object state)
        {
            dispatcher.Invoke(d, new object[] { state });
        }

        private void MainThread(object param)
        {
            Thread.VolatileWrite(ref dispObj, Dispatcher.CurrentDispatcher);
            Console.WriteLine("Main Thread is setup ! Id = {0}", Thread.CurrentThread.ManagedThreadId);
            Dispatcher.Run();
        }

        public void Dispose()
        {
            if (!dispatcher.HasShutdownStarted && !dispatcher.HasShutdownFinished)
                dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);

            GC.SuppressFinalize(this);
        }

        ~STASynchronizationContext()
        {
            Dispose();
        }
    }
}

这篇关于寻找一个自定义的SynchronizationContext的例子(用于单元测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆