码迷,mamicode.com
首页 > Windows程序 > 详细

网站后台调用winform MessageLoopApartment

时间:2014-12-16 15:02:07      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   ar   io   color   os   sp   

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

namespace AnfleCrawler.Common
{
    /// <summary>
    /// more info: http://stackoverflow.com/a/21808747/1768303
    /// </summary>
    public class MessageLoopApartment : IDisposable
    {
        /// <summary>
        /// the STA thread
        /// </summary>
        private Thread _thread;
        private TaskScheduler _taskScheduler;

        /// <summary>
        /// the STA thread‘s task scheduler
        /// </summary>
        public TaskScheduler TaskScheduler
        {
            get { return _taskScheduler; }
        }

        /// <summary>
        /// MessageLoopApartment constructor
        /// </summary>
        public MessageLoopApartment()
        {
            var tcs = new TaskCompletionSource<TaskScheduler>();
            // start an STA thread and gets a task scheduler
            _thread = new Thread(startArg =>
            {
                EventHandler idleHandler = null;
                idleHandler = (s, e) =>
                {
                    // handle Application.Idle just once
                    Application.Idle -= idleHandler;
                    // return the task scheduler
                    tcs.SetResult(TaskScheduler.FromCurrentSynchronizationContext());
                };

                // handle Application.Idle just once
                // to make sure we‘re inside the message loop
                // and SynchronizationContext has been correctly installed
                Application.Idle += idleHandler;
                Application.Run();
            });
            _thread.IsBackground = true;
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
            _taskScheduler = tcs.Task.Result;
        }
        /// <summary>
        /// Shutdown the STA thread
        /// </summary>
        public void Dispose()
        {
            if (_taskScheduler != null)
            {
                var taskScheduler = _taskScheduler;
                _taskScheduler = null;

                // execute Application.ExitThread() on the STA thread
                Task.Factory.StartNew(Application.ExitThread, CancellationToken.None, TaskCreationOptions.None, taskScheduler).Wait();
                _thread.Join();
                _thread = null;
            }
        }

        public void Invoke(Action func)
        {
            Task.Factory.StartNew(func, CancellationToken.None, TaskCreationOptions.None, _taskScheduler).Wait();
        }
        public TResult Invoke<TResult>(Func<TResult> func)
        {
            return Task.Factory.StartNew(func, CancellationToken.None, TaskCreationOptions.None, _taskScheduler).Result;
        }

        internal void Invoke(Action<object> func, object state)
        {
            Task.Factory.StartNew(func, state, CancellationToken.None, TaskCreationOptions.None, _taskScheduler);
        }
    }
}

 

网站后台调用winform MessageLoopApartment

标签:winform   style   blog   http   ar   io   color   os   sp   

原文地址:http://www.cnblogs.com/Googler/p/4167049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!