C#多線程Thread使用示例詳解
本文實例為大家分享了C#多線程Thread使用的示例代碼,供大家參考,具體內(nèi)容如下
多線程:

線程生命周期狀態(tài)圖:

C#線程優(yōu)先級(概率高低):


基本使用示例:
using System;
using System.Threading;
namespace month_9_10._1009
{
class Run5
{
/* 測試線程的調(diào)用過程
* 主線程輸出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:窗體標(biāo)簽循環(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 + "--票數(shù):" + _num--);
// }
//}
//放置監(jiān)視器
Monitor.Enter(this);
if (_num > 0)
{
Thread.Sleep(100);
print(Thread.CurrentThread.Name + "--票數(shù):" + _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();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
這篇文章介紹了C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
詳解Unity中Mask和RectMask2D組件的對比與測試
本篇文章給大家介紹Unity中Mask和RectMask2D組件的對比與測試,包括組件用法及RectMask2D的基本用法,通過Mask的原理分析實例代碼相結(jié)合給大家講解的非常詳細(xì),需要的朋友參考下吧2021-06-06
C#使用SqlSugarClient進行數(shù)據(jù)訪問并實現(xiàn)了統(tǒng)一的批量依賴注入(示例代碼)
M為 BaseDto 請用C# 給出一個案例,支持不同表對應(yīng)不同的業(yè)務(wù)邏輯層,然后不同倉儲實例,不同表的業(yè)務(wù),都實現(xiàn)統(tǒng)一的批量依賴注入,下面通過示例給大家演示如何使用SqlSugarClient進行數(shù)據(jù)訪問,并實現(xiàn)了統(tǒng)一的批量依賴注入,感興趣的朋友跟隨小編一起看看吧2024-05-05
詳解C#中通過委托來實現(xiàn)回調(diào)函數(shù)功能的方法
這篇文章主要介紹了C#中通過委托來實現(xiàn)回調(diào)函數(shù)功能的方法,文中舉了一個典型的多線程回調(diào)程序?qū)嵗?需要的朋友可以參考下2016-04-04

