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

.NET中STAThread的使用詳解

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

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

而在開(kāi)發(fā)類(lèi)別庫(kù)的時(shí)候,如果要使用類(lèi)似的STA線(xiàn)程模型,可以使用下列的程序代碼提供的類(lèi)別來(lái)完成。

復(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的問(wèn)題分析

    asp.net模板引擎Razor中cacheName的問(wèn)題分析

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

    Entity Framework使用Code First模式管理存儲(chǔ)過(guò)程

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

    C#/.NET使用git命令行來(lái)操作git倉(cāng)庫(kù)的方法示例

    本文介紹使用 C# 編寫(xiě)一個(gè) .NET 程序來(lái)自動(dòng)化地使用 git 命令行來(lái)操作 git 倉(cāng)庫(kù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    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í)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • .Net MVC網(wǎng)站中配置文件的讀寫(xiě)

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

    這篇文章主要為大家詳細(xì)介紹了.Net MVC 網(wǎng)站中配置文件的讀寫(xiě),感興趣的小伙伴們可以參考一下
    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ù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法

    mysql安裝后.net程序運(yùn)行出錯(cuò)的解決方法

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

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

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

    .Net集合排序的一種高級(jí)玩法實(shí)例教程

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

    .net core整合log4net的解決方案

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

最新評(píng)論

信丰县| 抚州市| 尚义县| 黎川县| 岑溪市| 巨鹿县| 宁乡县| 阳春市| 贡觉县| 东莞市| 商河县| 宝山区| 紫云| 师宗县| 新乐市| 皋兰县| 盐源县| 漳浦县| 永安市| 香河县| 海原县| 鄂州市| 太原市| 大邑县| 江阴市| 北流市| 凉城县| 香格里拉县| 连州市| 海阳市| 蒲城县| 淮北市| 黄梅县| 南木林县| 新安县| 龙里县| 子洲县| 长顺县| 沙雅县| 拉萨市| 山阳县|