-
-
Save greatcodeeer/215aed7beb3c383651db to your computer and use it in GitHub Desktop.
C# 多线程 线程池
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Go_TEST为测试样例 | |
* Go_Core为线程核心执行代码 | |
* | |
* 正在运行的线程数量:RunningThreadNumbers | |
* 暂停线程:Thread_Statu_Pause = checkBox1.Checked; | |
* 暂停取消:Thread_Statu_Exit = true; | |
* 提取线程(标识符GUID为[textBox2.Text]值)返回值 | |
* for (int i = 0; i < ThreadNodeS_Pool.Count; i++) | |
{ | |
if (ThreadNodeS_Pool[i].GUIDS == textBox2.Text) | |
{ | |
if (ThreadNodeS_Pool[i].IsDone) | |
{ | |
Console.WriteLine("指定线程:" + ThreadNodeS_Pool[i].GUIDS + " 返回值:" + ThreadNodeS_Pool[i].ReturnS); | |
textBox1.AppendText("指定线程:" + ThreadNodeS_Pool[i].GUIDS + " 返回值:" + ThreadNodeS_Pool[i].ReturnS + Environment.NewLine); | |
} | |
else | |
{ | |
Console.WriteLine("指定线程:" + ThreadNodeS_Pool[i].GUIDS + "还在运行中,不能获取返回值"); | |
textBox1.AppendText("指定线程:" + ThreadNodeS_Pool[i].GUIDS + "还在运行中,不能获取返回值" + Environment.NewLine); | |
} | |
} | |
} | |
*/ | |
/// <summary> | |
/// 暂停线程 | |
/// </summary> | |
bool Thread_Statu_Pause = false; | |
/// <summary> | |
/// 中止线程 | |
/// </summary> | |
bool Thread_Statu_Exit = false; | |
/// <summary> | |
/// 正在运行的线程数量 | |
/// </summary> | |
public int RunningThreadNumbers = 0; | |
/// <summary> | |
/// 线程记录节点 | |
/// </summary> | |
class ThreadNodeS | |
{ | |
/// <summary> | |
/// 用以识别线程标识 | |
/// </summary> | |
string _GUIDS; | |
/// <summary> | |
/// 用以标识线程是否执行完毕 | |
/// </summary> | |
bool _Done = false; | |
/// <summary> | |
/// 任务完成时标记为Done_Before | |
/// 等到计时器处理完毕由计时器标记Done | |
/// </summary> | |
bool _Done_Before = false; | |
/// <summary> | |
/// 用以标识线程是否取消 | |
/// </summary> | |
bool _Cancel = false; | |
/// <summary> | |
/// 用以标识线程是否在运行 | |
/// </summary> | |
bool _Running = false; | |
/// <summary> | |
/// 传递的参数 | |
/// </summary> | |
string _ParaS; | |
public string GUIDS { get { return _GUIDS; } } | |
public string ParaS { get { return _ParaS; } } | |
public bool IsRunning { get { return _Running; } } | |
public bool IsDone { get { return _Done; } } | |
public bool IsDoneBefore { get { return _Done_Before; } } | |
public bool IsCancel { get { return _Cancel; } } | |
/// <summary> | |
/// 返回值 | |
/// </summary> | |
public string ReturnS { set; get; } | |
/// <summary> | |
/// 重载线程记录节点 | |
/// </summary> | |
/// <param name="GUID">识别码</param> | |
/// <param name="ParaS">参数</param> | |
public ThreadNodeS(string GUID = null, string ParaS = null) | |
{ | |
if (GUID == null) | |
{ | |
_GUIDS = Guid.NewGuid().ToString("N"); | |
} | |
else | |
{ | |
_GUIDS = GUID; | |
} | |
_ParaS = ParaS; | |
ReturnS = null; | |
} | |
/// <summary> | |
/// 启动线程执行 | |
/// </summary> | |
public void Start() { _Running = true; } | |
/// <summary> | |
/// 线程执行完毕 | |
/// (由计时器判定) | |
/// </summary> | |
public void Done() { _Done = true; } | |
/// <summary> | |
/// 线程执行完毕 | |
/// (由线程自判定) | |
/// </summary> | |
public void Done_Before() { _Done_Before = true; _Running = false; } | |
/// <summary> | |
/// 线程取消 | |
/// </summary> | |
public void Cancel() { _Cancel = true; _Running = false; } | |
} | |
/// <summary> | |
/// 线程记录节点集合 | |
/// </summary> | |
List<ThreadNodeS> ThreadNodeS_Pool = new List<ThreadNodeS>(); | |
/// <summary> | |
/// 线程状态查看定时器 | |
/// </summary> | |
System.Windows.Forms.Timer ThreadControlTimer = new System.Windows.Forms.Timer(); | |
/// <summary> | |
/// 启动线程 | |
/// </summary> | |
void ThreadGo() | |
{ | |
//刷新线程状态 | |
Thread_Statu_Pause = false; | |
Thread_Statu_Exit = false; | |
//加入控制事件 | |
ThreadControlTimer.Dispose(); | |
ThreadControlTimer = new System.Windows.Forms.Timer(); | |
ThreadControlTimer.Tick += TickTick; | |
ThreadControlTimer.Interval = 100; | |
ThreadControlTimer.Start(); | |
//启动线程 | |
for (int i = 0; i < ThreadNodeS_Pool.Count; i++) | |
{ | |
Thread oThread = new Thread(Go_Core); | |
oThread.IsBackground = true; | |
oThread.Start(ThreadNodeS_Pool[i]); | |
} | |
} | |
/// <summary> | |
/// 查看进度的定时器 | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
void TickTick(object sender, EventArgs e) | |
{ | |
bool AllDone = true; | |
int RunningTemp = 0; | |
for (int i = 0; i < ThreadNodeS_Pool.Count; i++) | |
{ | |
//初始化时线程的IsDoneBefore和IsDone都是False | |
//当线程执行完毕,IsDoneBefore为True,状态为期待定时器(TickTick)验收 | |
if (ThreadNodeS_Pool[i].IsDoneBefore) | |
{ | |
//当定时器确定线程执行完毕 | |
//如果IsDone状态为False,执行验收处理 | |
//验收结束时IsDone状态会被置为True,至此,这个线程的任务彻底结束 | |
if (!ThreadNodeS_Pool[i].IsDone) | |
{ | |
//验收处理 | |
//输出线程返回结果 | |
ThreadNodeS_Pool[i].Done(); | |
Console.WriteLine(ThreadNodeS_Pool[i].ReturnS); | |
AllDone = false; | |
} | |
} | |
else if (ThreadNodeS_Pool[i].IsRunning) | |
{ | |
RunningTemp++; | |
AllDone = false; | |
} | |
else | |
{ | |
//只要有线程还没有进入IsDoneBefore状态,说明还有存活线程 | |
AllDone = false; | |
} | |
} | |
RunningThreadNumbers = RunningTemp;//返回正在运行的线程数量 | |
if (AllDone) | |
{ | |
Console.WriteLine("线程全部执行完毕"); | |
ThreadControlTimer.Stop(); | |
} | |
} | |
/// <summary> | |
/// 延时函数 | |
/// </summary> | |
/// <param name="delayTime">需要延时多少秒</param> | |
/// <returns></returns> | |
bool Delay(int delayTime) | |
{ | |
DateTime now = DateTime.Now; | |
int s; | |
do | |
{ | |
TimeSpan spand = DateTime.Now - now; | |
s = spand.Seconds; | |
Application.DoEvents(); | |
} | |
while (s < delayTime); | |
return true; | |
} | |
/// <summary> | |
/// 线程核心任务 | |
/// </summary> | |
/// <param name="Para"></param> | |
void Go_Core(object Para) | |
{ | |
ThreadNodeS SingleNode = (ThreadNodeS)Para; | |
#region 线程核心任务 | |
Int32 ALL = 0; | |
for (int i = 0; i < 200000; i++) | |
{ | |
//暂停线程 | |
do | |
{ | |
Delay(1); | |
} | |
while (Thread_Statu_Pause); | |
//取消线程 | |
if (Thread_Statu_Exit) | |
{ | |
SingleNode.Cancel(); | |
break; | |
} | |
ALL += i; | |
} | |
Delay(2); | |
SingleNode.ReturnS = "GUID:" + SingleNode.GUIDS + " Para:" + SingleNode.ParaS + " ALL:" + ALL + " Cancel:" + SingleNode.IsCancel; | |
#endregion | |
SingleNode.Done_Before(); | |
} | |
/// <summary> | |
/// 这是一个测试示例 | |
/// 测试执行默认标识(GUID)计算 | |
/// 测试提取针对标识(GUID)"TEST99"的返回值 | |
/// </summary> | |
void Go_TEST() | |
{ | |
//清空多线程节点集合 | |
ThreadNodeS_Pool.Clear(); | |
//设定多线程 | |
for (int i = 0; i < 10; i++) | |
{ | |
ThreadNodeS_Pool.Add(new ThreadNodeS(null, "Test -> " + i)); | |
} | |
ThreadNodeS_Pool.Add(new ThreadNodeS("TEST99", "Test -> 99")); | |
//多线程启动 | |
ThreadGo(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void TaskTest(object Para) | |
{ | |
Console.WriteLine(Para); | |
} | |
void ThreadPoolTest() | |
{ | |
WaitCallback WB = new WaitCallback(TaskTest); | |
ThreadPool.QueueUserWorkItem(WB, "Playing..."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment