日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程池,封装使用,实现控制子线程

發布時間:2024/4/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程池,封装使用,实现控制子线程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
類代碼 ? using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Threading;

namespace?TxWeb.CnForums
{
????public?class?TxThread
????{
????????public?delegate?void?Delegate(object?obj);

????????///?<summary>
????????
///?執行指定的方法,如果在指定的時間之內沒有完成,則中止
????????
///?</summary>
????????
///?<param?name="func">任務過程</param>
????????
///?<param?name="threadID">帖子編號</param>
????????
///?<param?name="timeSpan">超時時間</param>
????????
///?<param?name="timeoutCallback">如果超時,則調用該方法</param>
????????
///?<param?name="updateID">變化主表記錄編號</param>
????????
///?<returns>是否正確執行完畢</returns>
????????public?static?bool?Call(Delegate?func,?object?threadID,?TimeSpan?timeSpan,?Delegate?timeoutCallback,?object?updateID)
????????{
????????????if?(func?==?null)
????????????????throw?new?ArgumentNullException("func");

????????????ManualResetEvent?resetEvent?=?new?ManualResetEvent(false);
????????????ManualResetEvent?waitThreadEvent?=?new?ManualResetEvent(false);

????????????Exception?error?=?null;
????????????Thread?thread?=?null;

????????????//?將任務加到線程當中
????????????ThreadPool.QueueUserWorkItem(delegate
????????????{

????????????????thread?=?Thread.CurrentThread;
????????????????try?{?func(threadID);?}
????????????????catch?(ThreadAbortException)?{?}
????????????????catch?(Exception?ex)?{?error?=?ex;?}

????????????????resetEvent.Set();
????????????????//?每次線程執行結束都等待后續的處理邏輯
????????????????waitThreadEvent.WaitOne();
????????????});

????????????try
????????????{
????????????????//?等待任務的結束
????????????????bool?result?=?resetEvent.WaitOne(timeSpan,?false);

????????????????//?說明在執行過程中出現異常,直接拋出異常
????????????????if?(error?!=?null)
????????????????{
????????????????????throw?error;
????????????????}

????????????????if?(!result)
????????????????{
????????????????????if?(thread?!=?null)
????????????????????{
????????????????????????//?此時可以確保該線程沒有開始運行新的任務
????????????????????????thread.Abort();
????????????????????????waitThreadEvent.Set();
????????????????????}

????????????????????if?(timeoutCallback?!=?null)
????????????????????????timeoutCallback(updateID);
????????????????}

????????????????return?result;
????????????}
????????????finally
????????????{
????????????????//?最后確保釋放線程池線程
????????????????waitThreadEvent.Set();
????????????}
????????}
????}
} 調用: TxWeb.CnForums.TxThread.Delegate?createHtml?=?new?TxWeb.CnForums.TxThread.Delegate(CreateHtml);
????????????????TxWeb.CnForums.TxThread.Delegate?dealOverTime?=?new?TxWeb.CnForums.TxThread.Delegate(DealOverTime);
????????????????TxWeb.CnForums.TxThread.Call(createHtml,?threadID,?TimeSpan.FromSeconds(OverTime),?dealOverTime,?updateID);

轉載于:https://www.cnblogs.com/dajiang02/archive/2011/11/25/2263100.html

總結

以上是生活随笔為你收集整理的线程池,封装使用,实现控制子线程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。