最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET中STAThread的使用詳解

 更新時間:2013年05月04日 11:40:11   作者:  
這個STA線程模型,在線程內(nèi)加入了訊息幫浦等等機制,減少開發(fā)人員撰寫窗口程序的工作量

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

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

復(fù)制代碼 代碼如下:

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

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

        private readonly BlockingQueue<Action> _actionQueue = null;

        private Thread _thread = null;

        private ManualResetEvent _threadEvent = null;

        private ThreadState _threadState = ThreadState.Stopped;     

 
        // Constructor
        public STAThread()
        {
            // ActionQueue
            _actionQueue = new BlockingQueue<Action>();

            // 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();
            }
        }
    }
}

復(fù)制代碼 代碼如下:

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

        private readonly WaitHandle[] _waitHandles = null;

        private readonly Queue<T> _itemQueue = null;

        private readonly Semaphore _itemQueueSemaphore = null;

        private readonly ManualResetEvent _itemQueueReleaseEvent = null;

 
        // Constructors
        public BlockingQueue()
        {
            // Default
            _itemQueue = new Queue<T>();
            _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();
            }
        }
    }
}

相關(guān)文章

  • asp.net模板引擎Razor中cacheName的問題分析

    asp.net模板引擎Razor中cacheName的問題分析

    這篇文章主要介紹了asp.net模板引擎Razor中cacheName的問題,實例分析了cacheName在提高編譯效率方面的使用技巧,需要的朋友可以參考下
    2015-06-06
  • Entity Framework使用Code First模式管理存儲過程

    Entity Framework使用Code First模式管理存儲過程

    本文詳細(xì)講解了Entity Framework使用Code First模式管理存儲過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#/.NET使用git命令行來操作git倉庫的方法示例

    C#/.NET使用git命令行來操作git倉庫的方法示例

    本文介紹使用 C# 編寫一個 .NET 程序來自動化地使用 git 命令行來操作 git 倉庫。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • MVC使用Memcache+Cookie解決分布式系統(tǒng)共享登錄狀態(tài)學(xué)習(xí)筆記6

    MVC使用Memcache+Cookie解決分布式系統(tǒng)共享登錄狀態(tài)學(xué)習(xí)筆記6

    這篇文章主要介紹了MVC使用Memcache+Cookie解決分布式系統(tǒng)共享登錄狀態(tài)學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • .Net MVC網(wǎng)站中配置文件的讀寫

    .Net MVC網(wǎng)站中配置文件的讀寫

    這篇文章主要為大家詳細(xì)介紹了.Net MVC 網(wǎng)站中配置文件的讀寫,感興趣的小伙伴們可以參考一下
    2016-08-08
  • ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)

    ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)

    這篇文章介紹了ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • mysql安裝后.net程序運行出錯的解決方法

    mysql安裝后.net程序運行出錯的解決方法

    這篇文章主要給大家介紹了關(guān)于mysql安裝后.net程序運行出錯的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02
  • 詳解在DevExpress程序中使用TreeList控件以及節(jié)點查詢的處理

    詳解在DevExpress程序中使用TreeList控件以及節(jié)點查詢的處理

    本篇文章主要介紹基于DevExpress的TreeList控件使用以及使用SearchControl對節(jié)點進(jìn)行查詢的操作,具有一定的參考價值,下面跟著小編一起來看下吧
    2016-12-12
  • .Net集合排序的一種高級玩法實例教程

    .Net集合排序的一種高級玩法實例教程

    這篇文章主要給大家介紹了關(guān)于.Net集合排序的一種高級玩法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • .net core整合log4net的解決方案

    .net core整合log4net的解決方案

    這篇文章主要給大家介紹了關(guān)于.net core整合log4net的解決方案,文中通過圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07

最新評論

邳州市| 驻马店市| 陈巴尔虎旗| 玛沁县| 江永县| 永宁县| 蕲春县| 仁怀市| 承德市| 通道| 黄骅市| 大埔区| 南部县| 集贤县| 郎溪县| 青岛市| 阳江市| 建始县| 德州市| 乌拉特后旗| 旌德县| 城口县| 永修县| 宝山区| 哈尔滨市| 滨海县| 新平| 广西| 万盛区| 繁昌县| 姚安县| 哈尔滨市| 皋兰县| 永登县| 大洼县| 垫江县| 伊通| 定安县| 东丽区| 东乡县| 且末县|