国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

在C#中編寫多線程應用程序

2024-07-21 02:19:03
字體:
來源:轉載
供稿:網友
以前在使用vb來實現多線程的時候,發現有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在c#中,要編寫多線程應用程序卻相當的簡單。這篇文章將作簡要的介紹,以起到拋磚引玉的作用!
.net將關于多線程的功能定義在system.threading名字空間中。因此,要使用多線程,必須先聲明引用此名字空間(using system.threading;)。
即使你沒有編寫多線程應用程序的經驗,也可能聽說過“啟動線程”“殺死線程”這些詞,其實除了這兩個外,涉及多線程方面的還有諸如“暫停線程”“優先級”“掛起線程”“恢復線程”等等。下面將一個一個的解釋。
a.啟動線程
顧名思義,“啟動線程”就是新建并啟動一個線程的意思,如下代碼可實現:
thread thread1 = new thread(new threadstart( count));
其中的 count 是將要被新線程執行的函數。
b.殺死線程
“殺死線程”就是將一線程斬草除根,為了不白費力氣,在殺死一個線程前最好先判斷它是否還活著(通過 isalive 屬性),然后就可以調用 abort 方法來殺死此線程。
c.暫停線程
它的意思就是讓一個正在運行的線程休眠一段時間。如 thread.sleep(1000); 就是讓線程休眠1秒鐘。
d.優先級
這個用不著解釋了。thread類中幸桓鯰hreadpriority屬性,它用來設置優先級,但不能保證操作系統會接受該優先級。一個線程的優先級可分為5種:normal, abovenormal, belownormal, highest, lowest。具體實現例子如下:
thread.priority = threadpriority.highest;
e.掛起線程
thread類的suspend方法用來掛起線程,知道調用resume,此線程才可以繼續執行。如果線程已經掛起,那就不會起作用。
if (thread.threadstate = threadstate.running)
{
thread.suspend();
}
f.恢復線程
用來恢復已經掛起的線程,以讓它繼續執行,如果線程沒掛起,也不會起作用。
if (thread.threadstate = threadstate.suspended)
{
thread.resume();
}
下面將列出一個例子,以說明簡單的線程處理功能。此例子來自于幫助文檔。
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();
}
}

此代碼產生的輸出類似如下內容:

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.



菜鳥學堂:
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 余干县| 贵州省| 肥西县| 巴东县| 长顺县| 开封市| 凌海市| 大英县| 新营市| 平阳县| 岑巩县| 尼玛县| 高雄县| 肇庆市| 绥芬河市| 崇信县| 策勒县| 海门市| 奎屯市| 麻城市| 卓资县| 凯里市| 冀州市| 霍邱县| 蒲江县| 中卫市| 华亭县| 宣威市| 三穗县| 江口县| 岑巩县| 安丘市| 望奎县| 应用必备| 岳普湖县| 通山县| 濮阳县| 石河子市| 临漳县| 西乌珠穆沁旗| 木兰县|