C#停止線(xiàn)程的方法
本文實(shí)例講述了C#停止線(xiàn)程的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinFormApp { public partial class Form1 : Form { System.Threading.CancellationTokenSource cancel = new System.Threading.CancellationTokenSource(); System.Threading.Thread[] thread; int len = 2; public Form1() { InitializeComponent(); thread = new System.Threading.Thread[len]; } void RunThread() { ThreadInvoke.SetEventInvokeValue(richTextBox1, "即將開(kāi)始運(yùn)行線(xiàn)程."); System.Threading.Thread t = null; for (int i = 0; i < len; i++) { t = new System.Threading.Thread(new System.Threading.ThreadStart(Sample)); t.Name = "thread_0" + i.ToString(); t.IsBackground = true; thread.SetValue(t, i); t.Start(); } } void Sample() { string name = System.Threading.Thread.CurrentThread.Name; ThreadInvoke.SetEventInvokeValue(richTextBox1, "正在運(yùn)行線(xiàn)程:" + name); while (true) { if (cancel.IsCancellationRequested) { ThreadInvoke.SetEventInvokeValue(richTextBox1, "線(xiàn)程:" + name + " 停止運(yùn)行..."); //線(xiàn)程被終止后回調(diào) cancel.Token.Register(delegate { ThreadInvoke.SetEventInvokeValue(richTextBox1, "線(xiàn)程:" + name + " 停止運(yùn)行之后的回調(diào)函數(shù)..."); }); break; } } } void ShowStatu() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { if (thread[i].IsAlive == true) { sb.AppendLine("線(xiàn)程:" + thread[i].Name.ToString() + " 還在運(yùn)行..."); } } if (sb.ToString() == "") { sb.AppendLine("線(xiàn)程已經(jīng)全部停止..."); } richTextBox1.Text += sb.ToString(); } /// <summary> /// 開(kāi)始運(yùn)行線(xiàn)程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { RunThread(); } /// <summary> /// 顯示所有的線(xiàn)程狀態(tài) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { ShowStatu(); } /// <summary> /// 終止所有的線(xiàn)程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { cancel.Cancel(); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#在Unity游戲開(kāi)發(fā)中進(jìn)行多線(xiàn)程編程的方法
- C#線(xiàn)程處理系列之線(xiàn)程池中的I/O線(xiàn)程
- 解析C#多線(xiàn)程編程中異步多線(xiàn)程的實(shí)現(xiàn)及線(xiàn)程池的使用
- C#多線(xiàn)程編程之使用ReaderWriterLock類(lèi)實(shí)現(xiàn)多用戶(hù)讀與單用戶(hù)寫(xiě)同步的方法
- C#基于委托實(shí)現(xiàn)多線(xiàn)程之間操作的方法
- C#實(shí)現(xiàn)向多線(xiàn)程傳參的三種方式實(shí)例分析
- C#線(xiàn)程隊(duì)列用法實(shí)例分析
- C#中前臺(tái)線(xiàn)程和后臺(tái)線(xiàn)程的區(qū)別與聯(lián)系
- C#在子線(xiàn)程中更新窗口部件的寫(xiě)法
- 簡(jiǎn)單對(duì)比C#程序中的單線(xiàn)程與多線(xiàn)程設(shè)計(jì)
相關(guān)文章
C#上位機(jī)與三菱PLC通訊的實(shí)現(xiàn)步驟(圖文)
這篇文章主要介紹了C#上位機(jī)與三菱PLC通訊的實(shí)現(xiàn)步驟(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02C#開(kāi)發(fā)Android百度地圖手機(jī)應(yīng)用程序(多地圖展示)
這篇文章主要介紹了C#開(kāi)發(fā)Android百度地圖手機(jī)應(yīng)用程序(多地圖展示)的相關(guān)資料,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#下載網(wǎng)頁(yè)并在控制臺(tái)輸出的方法
這篇文章主要介紹了C#下載網(wǎng)頁(yè)并在控制臺(tái)輸出的方法,涉及C#基于http協(xié)議進(jìn)行網(wǎng)頁(yè)抓取及控制臺(tái)輸出顯示的相關(guān)技巧,需要的朋友可以參考下2015-07-07精簡(jiǎn)高效的C#網(wǎng)站優(yōu)化經(jīng)驗(yàn)技巧總結(jié)
這篇文章主要為大家介紹了精簡(jiǎn)高效的C#網(wǎng)站優(yōu)化經(jīng)驗(yàn)技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04C#中的lock、Monitor、Mutex學(xué)習(xí)筆記
這篇文章主要介紹了C#中的lock、Monitor、Mutex學(xué)習(xí)筆記,本文講解的都是線(xiàn)程同步的一些知識(shí),需要的朋友可以參考下2015-01-01