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

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

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


基本使用示例:
using System;
using System.Threading;
namespace month_9_10._1009
{
class Run5
{
/* 測(cè)試線程的調(diào)用過(guò)程
* 主線程輸出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}");
}
}
}
}
實(shí)例1:窗體標(biāo)簽循環(huán)滾動(dòng)

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();
}
}
}
}
實(shí)例2:進(jìn)度條

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();
}
}
}
實(shí)例三:線程同步(售票模擬)
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();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 通過(guò)Socket讀取大量數(shù)據(jù)的示例
這篇文章主要介紹了C# 通過(guò)Socket讀取大量數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程
這篇文章主要介紹了C#實(shí)現(xiàn)控制線程池最大數(shù)并發(fā)線程的相關(guān)資料,需要的朋友可以參考下2016-07-07
C# windows語(yǔ)音識(shí)別與朗讀實(shí)例
這篇文章主要為大家詳細(xì)介紹了C# windows語(yǔ)音識(shí)別與朗讀實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C#編程調(diào)用Cards.dll實(shí)現(xiàn)圖形化發(fā)牌功能示例
這篇文章主要介紹了C#編程調(diào)用Cards.dll實(shí)現(xiàn)圖形化發(fā)牌功能,結(jié)合實(shí)例形式分析了C#動(dòng)態(tài)鏈接庫(kù)調(diào)用及圖形操作技巧,需要的朋友可以參考下2017-06-06

