執行緒被定義為程式的執行路徑。每個執行緒都定義了乙個獨特的控制流。如果您的應用程式涉及到複雜的和耗時的操作,那麼設定不同的執行緒執行路徑往往是有益的,每個執行緒執行特定的工作。
執行緒是輕量級程序。乙個使用執行緒的常見例項是現代作業系統中並行程式設計的實現。使用執行緒節省了 cpu 週期的浪費,同時提高了應用程式的效率。
到目前為止我們編寫的程式是乙個單執行緒作為應用程式的執行例項的單一的過程執行的。但是,這樣子應用程式同時只能執行乙個任務。為了同時執行多個任務,它可以被劃分為更小的執行緒。
執行緒生命週期開始於 system.threading.thread 類的物件被建立時,結束於執行緒被終止或完成執行時。
下面列出了執行緒生命週期中的各種狀態:
死亡狀態:當執行緒已完成執行或已中止時的狀況。
在 c# 中,system.threading.thread類用於執行緒的工作。它允許建立並訪問多執行緒應用程式中的單個執行緒。程序中第乙個被執行的執行緒稱為主線程。
當 c# 程式開始執行時,主線程自動建立。使用thread類建立的執行緒被主線程的子執行緒呼叫。您可以使用 thread 類的currentthread屬性訪問執行緒。
下面的程式演示了主線程的執行:
using當上面的**被編譯和執行時,它會產生下列結果:system
;using
system
.threading
;namespace",
th.name
);console
.readkey
();}
}}
this執行緒是通過擴充套件 thread 類建立的。擴充套件的 thread 類呼叫start()方法來開始子執行緒的執行。ismainthread
下面的程式演示了這個概念:
using當上面的**被編譯和執行時,它會產生下列結果:system
;using
system
.threading
;namespace
static
void
main
(string
args)}
}
inthread 類提供了各種管理執行緒的方法。main
:creating
the
child
thread
child
thread starts
下面的例項演示了sleep()方法的使用,用於在乙個特定的時間暫停執行緒。
using當上面的**被編譯和執行時,它會產生下列結果:system
;using
system
.threading
;namespace
seconds"
,sleepfor
/1000
);thread
.sleep
(sleepfor
);console
.writeline
("child thread resumes");}
static
void
main
(string
args)}
}
inabort()方法用於銷毀執行緒。main
:creating
the
child
thread
child
thread starts
child
thread
paused
for5
seconds
child
thread resumes
通過丟擲threadabortexception在執行時中止執行緒。這個異常不能**獲,如果有 finally 塊,控制會被送至 finally 塊。
下面的程式說明了這點:
using當上面的**被編譯和執行時,它會產生下列結果:system
;using
system
.threading
;namespace
console
.writeline
("child thread completed");}
catch
(threadabortexceptione)
finally
}static
void
main
(string
args)}
}
inmain
:creating
the
child
thread
child
thread starts01
2inmain
:aborting
the
child
thread
thread
abort
exception
couldn
't catch the thread exception
執行緒函式通過委託傳遞,可以不帶引數,也可以帶引數(只能有乙個引數),可以用乙個類或結構體封裝引數:
usingsystem
;using
system
.threading
;namespace
test
public
static
void
testmethod
()public
static
void
testmethod
(object
data)"
,datastr);}
}}
(1)執行緒自動銷毀
threada=執行緒方法method執行完結,執行緒a也自動終止.newthread
(method)th
.start
();
(2)如果是無限迴圈需要手動銷毀
窗體應用程式的執行緒間通訊
.net 2.0以後加強了安全機制,不允許在winform中直接跨執行緒訪問控制項的屬性。
執行緒函式通過委託傳遞,是一種安全的執行緒間通訊的方式。也是委託的功能之一。
但c#也可以關閉執行緒安全保護,自由的呼叫其他執行緒生成的控制項。只要加上
control但不推薦使用。自己練習時可以寫著玩一下。.checkforillegalcrossthreadcalls
=false
;
本篇文章的第二段**建立執行緒中,在2.0以後可以直接執行子執行緒,這樣一來程式可以省略main函式中的第一行**。執行結果一樣,似乎程式會更簡單易懂點。如下:
usingsystem
.threading
.tasks
;using
system
.threading
;namespace
static
void
main
(string
args)}
}
c# 在 4.0 以後一共有3種建立執行緒的方式:
classprogram
private
static
void
threadprocess
(object
tag) ",
i));
thread
.sleep(10
);i--;}
}}
多執行緒基礎知識
建立乙個序列佇列,該佇列中從方的都是要依次執行的任務,dispatch queue serial 表示序列佇列的標示 dispatch queue t serialqueue dispatch queue create serial dispatch queue serial 建立乙個並行佇列,並行...
多執行緒 基礎知識
1 建立執行緒 extends thread implements runnable 啟動執行緒 threadl類的start 執行緒完成 1 run 方法執行完成 2 丟擲乙個未處理的異常導致執行緒的提前結束 2 執行緒的狀態 新建立 執行緒被建立,但是沒有呼叫start方法 可執行 runnab...
C 多執行緒基礎知識彙總
最近自己寫了個小爬蟲,裡面用到了多執行緒技術,忽然發現對此技術竟然有些陌生了,於是乎開始瘋狂的去問度娘,在此記錄下來,以便自己和各位小夥伴們學習。一 什麼是執行緒 乙個應用程式就相當於乙個程序,程序擁有應用程式的所有資源程序包括執行緒,程序的資源被執行緒共享,但不擁有執行緒。我們可以開啟電腦中的任務...