C#多線程Thread使用示例詳解
更新時間:2021年07月11日 17:27:34 作者:空山新雨
這篇文章主要為大家詳細介紹了C#多線程Thread使用示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#多線程Thread使用的示例代碼,供大家參考,具體內容如下
多線程:
線程生命周期狀態(tài)圖:
C#線程優(yōu)先級(概率高低):
基本使用示例:
using System; using System.Threading; namespace month_9_10._1009 { class Run5 { /* 測試線程的調用過程 * 主線程輸出world,子線程輸出hello */ public static void showHello() { for(int i = 0; i < 100; i++) { Console.WriteLine($"Hello\t#{Thread.CurrentThread.Name}"); } } public static void Main(string[] args) { Thread.CurrentThread.Name = "Main Thread!"; var childThreadRef = new ThreadStart(showHello); Console.WriteLine("This is Main process!!!"); var childThread = new Thread(childThreadRef); childThread.Name = "Child Thread!"; childThread.Start(); for (int i = 0; i < 100; i++) { Console.WriteLine($"World!\t#{Thread.CurrentThread.Name}"); } } } }
實例1:窗體標簽循環(huán)滾動
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace RollMove { public partial class Form1 : Form { Thread th1 = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int _sx = 40; int _ex = 280; int _top = 70; th1 = new Thread(() => { while (true) { if (_sx <= _ex) { _ex = 280; label1.Location = new Point(_sx, _top); Thread.Sleep(20); _sx += 5; } else { _ex = 40; label1.Location = new Point(_sx, _top); Thread.Sleep(20); _sx -= 5; } } }); th1.Start(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (th1!=null) { th1.Abort(); } } } }
實例2:進度條
using System; using System.Threading; using System.Windows.Forms; namespace month_9_10._1012 { public partial class Form_3 : Form { public static Print.Print print = Console.WriteLine; Thread th1, th2; public Form_3() { InitializeComponent(); Text = "Welcome!"; CheckForIllegalCrossThreadCalls = false; th1 = new Thread(new ThreadStart(Pro1)); th1.Priority = ThreadPriority.Lowest; th1.Start(); th2 = new Thread(new ThreadStart(Pro2)); th2.Priority = ThreadPriority.Highest; //th2.Start(); print(th1.Priority); print(th2.Priority); } void Pro1() { print("XXXXXXX"); for (int i = 0; i <= 100; i++) { progressBar1.Value = i; label1.Text = $"{progressBar1.Value}%"; Thread.Sleep(200); if (i == 20) { th2.Start(); th2.Join(); } } } void Pro2() { print("YYYYYYYY"); for (int i = 0; i <= 100; i++) { progressBar2.Value = i; label2.Text = $"{progressBar2.Value}%"; Thread.Sleep(100); } } private void progressBar1_Click(object sender, EventArgs e) { print("This is Main threading!"); } private void Form_3_Load(object sender, EventArgs e) { } private void Form_3_FormClosing_1(object sender, FormClosingEventArgs e) { if (th1.ThreadState == ThreadState.Running) th1.Abort(); if (th2.ThreadState == ThreadState.Running) th2.Abort(); } } }
實例三:線程同步(售票模擬)
using System; using System.Threading; namespace month_9_10._1012 { class Run2 { static Print.Print print = Console.WriteLine; int _num = 10; void Ticket() { while (true) { //上鎖 //lock (this) //{ // if (_num > 0) // { // Thread.Sleep(100); // print(Thread.CurrentThread.Name + "--票數:" + _num--); // } //} //放置監(jiān)視器 Monitor.Enter(this); if (_num > 0) { Thread.Sleep(100); print(Thread.CurrentThread.Name + "--票數:" + _num--); } Monitor.Exit(this); } } static void Main() { var a1 = new Run2(); Thread t1 = new Thread(new ThreadStart(a1.Ticket)); t1.Name = "線程一"; Thread t2 = new Thread(new ThreadStart(a1.Ticket)); t2.Name = "線程二"; Thread t3 = new Thread(new ThreadStart(a1.Ticket)); t3.Name = "線程三"; Thread t4 = new Thread(new ThreadStart(a1.Ticket)); t4.Name = "線程四"; t1.Start(); t2.Start(); t3.Start(); t4.Start(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解Unity中Mask和RectMask2D組件的對比與測試
本篇文章給大家介紹Unity中Mask和RectMask2D組件的對比與測試,包括組件用法及RectMask2D的基本用法,通過Mask的原理分析實例代碼相結合給大家講解的非常詳細,需要的朋友參考下吧2021-06-06C#使用SqlSugarClient進行數據訪問并實現了統(tǒng)一的批量依賴注入(示例代碼)
M為 BaseDto 請用C# 給出一個案例,支持不同表對應不同的業(yè)務邏輯層,然后不同倉儲實例,不同表的業(yè)務,都實現統(tǒng)一的批量依賴注入,下面通過示例給大家演示如何使用SqlSugarClient進行數據訪問,并實現了統(tǒng)一的批量依賴注入,感興趣的朋友跟隨小編一起看看吧2024-05-05