好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > .NET中STAThread的使用詳解

.NET中STAThread的使用詳解

熱門標簽:鄭州400電話辦理 聯通 電銷語音自動機器人 戶外地圖標注軟件手機哪個好用 萊蕪外呼電銷機器人價格 長春呼叫中心外呼系統哪家好 五常地圖標注 地圖標注和認領 凱立德導航官網地圖標注 智能電話營銷外呼系統

在WindowForm應用程序中主要的線程,是采用一種稱為「Single-Threaded Apartment(STA)」的線程模型。這個STA線程模型,在線程內加入了訊息幫浦等等機制,減少開發人員撰寫窗口程序的工作量。
 

而在開發類別庫的時候,如果要使用類似的STA線程模型,可以使用下列的程序代碼提供的類別來完成。

復制代碼 代碼如下:

namespace CLK.Threading
{
    public class STAThread
    {
        // Enum
        private enum ThreadState
        {
            Started,
            Stopping,
            Stopped,
        }

 
        // Fields
        private readonly object _syncRoot = new object();

        private readonly BlockingQueueAction> _actionQueue = null;

        private Thread _thread = null;

        private ManualResetEvent _threadEvent = null;

        private ThreadState _threadState = ThreadState.Stopped;     

 
        // Constructor
        public STAThread()
        {
            // ActionQueue
            _actionQueue = new BlockingQueueAction>();

            // ThreadEvent
            _threadEvent = new ManualResetEvent(true);

            // ThreadState
            _threadState = ThreadState.Stopped;     
        }

 
        // Methods
        public void Start()
        {          
            // Sync
            lock (_syncRoot)
            {
                // ThreadState
                if (_threadState != ThreadState.Stopped) throw new InvalidOperationException();
                _threadState = ThreadState.Started;
            }

            // Thread
            _thread = new Thread(this.Operate);
            _thread.Name = string.Format("Class:{0}, Id:{1}", "STAThread", _thread.ManagedThreadId);
            _thread.IsBackground = false;
            _thread.Start();
        }

        public void Stop()
        {
            // Sync
            lock (_syncRoot)
            {
                // ThreadState
                if (_threadState != ThreadState.Started) throw new InvalidOperationException();
                _threadState = ThreadState.Stopping;

                // ActionQueue
                _actionQueue.Release();
            }

            // Wait
            _threadEvent.WaitOne();
        }

 
        public void Post(SendOrPostCallback callback, object state)
        {
            #region Contracts

            if (callback == null) throw new ArgumentNullException();

            #endregion

            // Action
            Action action = delegate()
            {
                try
                {
                    callback(state);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Delegate:{0}, State:{1}, Message:{2}", callback.GetType(), "Exception", ex.Message));
                }
            };

            // Sync
            lock (_syncRoot)
            {
                // ThreadState
                if (_threadState != ThreadState.Started) throw new InvalidOperationException();

                // ActionQueue
                _actionQueue.Enqueue(action);
            }                     
        }

        public void Send(SendOrPostCallback callback, object state)
        {
            #region Contracts

            if (callback == null) throw new ArgumentNullException();

            #endregion

            // Action
            ManualResetEvent actionEvent = new ManualResetEvent(false);
            Action action = delegate()
            {
                try
                {
                    callback(state);
                }
                catch (Exception ex)
                {
                    Debug.Fail(string.Format("Delegate:{0}, State:{1}, Message:{2}", callback.GetType(), "Exception", ex.Message));
                }
                finally
                {
                    actionEvent.Set();
                }
            };

            // Sync
            lock (_syncRoot)
            {
                // ThreadState
                if (_threadState != ThreadState.Started) throw new InvalidOperationException();

                // ActionQueue
                if (Thread.CurrentThread != _thread)
                {
                    _actionQueue.Enqueue(action);
                }
            }

            // Execute
            if (Thread.CurrentThread == _thread)
            {
                action();
            }

            // Wait
            actionEvent.WaitOne();
        }

 
        private void Operate()
        {
            try
            {
                // Begin
                _threadEvent.Reset();

                // Operate
                while (true)
                {
                    // Action
                    Action action = _actionQueue.Dequeue();

                    // Execute
                    if (action != null)
                    {
                        action();
                    }

                    // ThreadState
                    if (action == null)
                    {
                        lock (_syncRoot)
                        {
                            if (_threadState == ThreadState.Stopping)
                            {
                                return;
                            }
                        }
                    }
                }
            }
            finally
            {
                // End
                lock (_syncRoot)
                {
                    _threadState = ThreadState.Stopped;
                }
                _threadEvent.Set();
            }
        }
    }
}

復制代碼 代碼如下:

namespace CLK.Threading
{
    public class BlockingQueueT>
    {
        // Fields      
        private readonly object _syncRoot = new object();

        private readonly WaitHandle[] _waitHandles = null;

        private readonly QueueT> _itemQueue = null;

        private readonly Semaphore _itemQueueSemaphore = null;

        private readonly ManualResetEvent _itemQueueReleaseEvent = null;

 
        // Constructors
        public BlockingQueue()
        {
            // Default
            _itemQueue = new QueueT>();
            _itemQueueSemaphore = new Semaphore(0, int.MaxValue);
            _itemQueueReleaseEvent = new ManualResetEvent(false);
            _waitHandles = new WaitHandle[] { _itemQueueSemaphore, _itemQueueReleaseEvent };
        }

 
        // Methods
        public void Enqueue(T item)
        {
            lock (_syncRoot)
            {
                _itemQueue.Enqueue(item);
                _itemQueueSemaphore.Release();
            }
        }

        public T Dequeue()
        {
            WaitHandle.WaitAny(_waitHandles);
            lock (_syncRoot)
            {
                if (_itemQueue.Count > 0)
                {
                    return _itemQueue.Dequeue();
                }
            }
            return default(T);
        }

        public void Release()
        {
            lock (_syncRoot)
            {
                _itemQueueReleaseEvent.Set();
            }
        }

        public void Reset()
        {
            lock (_syncRoot)
            {
                _itemQueue.Clear();
                _itemQueueSemaphore.Close();
                _itemQueueReleaseEvent.Reset();
            }
        }
    }
}

您可能感興趣的文章:
  • .NET Windows 多線程thread編程
  • 深入多線程之:解析線程的交會(Thread Rendezvous)詳解
  • C++實現CreatThread函數主線程與工作線程交互的方法
  • C++線程優先級SetThreadPriority的使用實例
  • C++封裝遠程注入類CreateRemoteThreadEx實例
  • C++ AfxBeginThread的介紹/基本用法

標簽:宣城 岳陽 湖州 衢州 西寧 紅河 西藏 福州

巨人網絡通訊聲明:本文標題《.NET中STAThread的使用詳解》,本文關鍵詞  .NET,中,STAThread,的,使用,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.NET中STAThread的使用詳解》相關的同類信息!
  • 本頁收集關于.NET中STAThread的使用詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 亚洲,国产成人精品无码区| 屏边| 在婚房被伴郎c了2个小时| 欧美伊人久久大香线蕉综合69| 91精品国产色综合久久不卡98| 男男无删减做爰高H漫画| 老公帮我添添的水好多受不了视频| 内裤穿一天就有黄结痂| 强迫用肉体偿债的已婚妇女电影 | japanese黑人高清| 国内精品久久久久精品综合紧身裙| 欧美极品少妇XXXXⅩOO电影| 曰批全过程免费视频好爽| 七仙女裸体被强开双腿小说| 精品国产欧美另类一区| 国产人妻AV精品一区二区三区| 男人边吃奶边爱边做视频刺激| 亚洲国产精品美女久久久久AV| 艳降勾魂未删减完整版下载bt| 丝瓜黄瓜视频在线观看视频| 麻豆精品无码国产在线播| 插插插色综合| 厨房客厅征服美妇| 意大利xxxx极品| 精品国内自产拍在线观看视频| 午夜福利色| 久久国产免费观看精品3| 亚洲精品秘?一区二区三区在线观看| 午夜色女| 性感的邻居| 美女视频黄a视频专区一| 亚州免费一级毛片| 中文字幕亚洲综合精品一区| 关晓彤被调教出奶水| 久久99精品久久久学生| 男男激烈床戏视频大尺度| 人妻熟女一区二区三区| 顶级肉欲(高H、纯肉故事集| 污视频app有哪些| 公憩系列t老扒| 6080yy情影院理论片|