在C#中編寫(xiě)多線程應(yīng)用程序,容易!
發(fā)表時(shí)間:2024-01-22 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]來(lái)自:www.kunwsoft.com 以前在使用VB來(lái)實(shí)現(xiàn)多線程的時(shí)候,發(fā)現(xiàn)有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫(xiě)多線程應(yīng)用程序卻相當(dāng)?shù)暮?jiǎn)單。這篇文章將作簡(jiǎn)要的介紹,以起到拋磚引玉的作用! .NET將關(guān)于多線程的功能定義在System.Threadin...
來(lái)自:www.kunwsoft.com
以前在使用VB來(lái)實(shí)現(xiàn)多線程的時(shí)候,發(fā)現(xiàn)有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫(xiě)多線程應(yīng)用程序卻相當(dāng)?shù)暮?jiǎn)單。這篇文章將作簡(jiǎn)要的介紹,以起到拋磚引玉的作用!
.NET將關(guān)于多線程的功能定義在System.Threading名字空間中。因此,要使用多線程,必須先聲明引用此名字空間(using System.Threading;)。
即使你沒(méi)有編寫(xiě)多線程應(yīng)用程序的經(jīng)驗(yàn),也可能聽(tīng)說(shuō)過(guò)“啟動(dòng)線程”“殺死線程”這些詞,其實(shí)除了這兩個(gè)外,涉及多線程方面的還有諸如“暫停線程”“優(yōu)先級(jí)”“掛起線程”“恢復(fù)線程”等等。下面將一個(gè)一個(gè)的解釋。
a.啟動(dòng)線程
顧名思義,“啟動(dòng)線程”就是新建并啟動(dòng)一個(gè)線程的意思,如下代碼可實(shí)現(xiàn):
Thread thread1 = new Thread(new ThreadStart( Count));
其中的 Count 是將要被新線程執(zhí)行的函數(shù)。
b.殺死線程
“殺死線程”就是將一線程斬草除根,為了不白費(fèi)力氣,在殺死一個(gè)線程前最好先判斷它是否還活著(通過(guò) IsAlive 屬性),然后就可以調(diào)用 Abort 方法來(lái)殺死此線程。
c.暫停線程
它的意思就是讓一個(gè)正在運(yùn)行的線程休眠一段時(shí)間。如 thread.Sleep(1000); 就是讓線程休眠1秒鐘。
d.優(yōu)先級(jí)
這個(gè)用不著解釋了。Thread類(lèi)中有一個(gè)ThreadPriority屬性,它用來(lái)設(shè)置優(yōu)先級(jí),但不能保證操作系統(tǒng)會(huì)接受該優(yōu)先級(jí)。一個(gè)線程的優(yōu)先級(jí)可分為5種:Normal, AboveNormal, BelowNormal, Highest, Lowest。具體實(shí)現(xiàn)例子如下:
thread.Priority = ThreadPriority.Highest;
e.掛起線程
Thread類(lèi)的Suspend方法用來(lái)掛起線程,知道調(diào)用Resume,此線程才可以繼續(xù)執(zhí)行。如果線程已經(jīng)掛起,那就不會(huì)起作用。
if (thread.ThreadState = ThreadState.Running)
{
thread.Suspend();
}
f.恢復(fù)線程
用來(lái)恢復(fù)已經(jīng)掛起的線程,以讓它繼續(xù)執(zhí)行,如果線程沒(méi)掛起,也不會(huì)起作用。
if (thread.ThreadState = ThreadState.Suspended)
{
thread.Resume();
}
下面將列出一個(gè)例子,以說(shuō)明簡(jiǎn)單的線程處理功能。此例子來(lái)自于幫助文檔。
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
此代碼產(chǎn)生的輸出類(lèi)似如下內(nèi)容:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.