zoukankan      html  css  js  c++  java
  • 网站后台调用winform MessageLoopApartment

    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);
            }
        }
    }
  • 相关阅读:
    SequoiaDB数据库集群部署
    初步了解SequoiaDB数据库
    SequoiaDB数据库的一般概念介绍
    SequoiaDB(巨杉数据库)(社区版)安装配置使用图解
    记录用Django搭建博客的问题之一:SITE_ID=1
    解决问题七步法
    python的浅拷贝和深copy
    Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中 (转)
    用PYTHON监听鼠标和键盘事件(转)
    转:python中range和xrange的区别
  • 原文地址:https://www.cnblogs.com/Googler/p/4167049.html
Copyright © 2011-2022 走看看