0% found this document useful (0 votes)
99 views

How To - Create and Terminate Threads (C#)

The document discusses creating and terminating threads in C# programming. It explains how to create a thread class that derives from Thread and override its Run method. The document also covers how to start a thread, wait for it to finish using Join, and terminate it. An example program is provided that demonstrates creating two thread classes that write to the console and starting, joining and terminating the threads.

Uploaded by

Bruno Malka
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

How To - Create and Terminate Threads (C#)

The document discusses creating and terminating threads in C# programming. It explains how to create a thread class that derives from Thread and override its Run method. The document also covers how to start a thread, wait for it to finish using Join, and terminate it. An example program is provided that demonstrates creating two thread classes that write to the console and starting, joining and terminating the threads.

Uploaded by

Bruno Malka
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

17/01/12

Ho

o: C ea e and Te mina e Th ead (C#)

Ho to: Create and Terminate Threads (C# Programming Guide)


Vi T
1

al S

dio 2005 . M . F (C# P


Wre okr

G ) 2. . T

M T

DWr. T ook

, :

Mi an

DWr ook

C# pbi vi DWr( ulc od ook) { wie(_hudtp hl !solSo) { CnoeWieie"okrtra:wrig."; osl.rtLn(wre hed okn..) } CnoeWieie"okrtra:triaiggaeul.) osl.rtLn(wre hed emntn rcfly"; }

Wre okr

. T

Rqettp eusSo,

DWr ook

C# pbi vi Rqettp) ulc od eusSo( { _hudtp tu; solSo re }

Rqettp eusSo DWr ook

, . H

_hudtp solSo

e. B
DWr ook , DWr ook Rqettp eusSo , Rqettp eusSo

, C#

. DWr ook
_hudtp solSo

ola ile,

piaevltl bo _hudtp rvt oaie ol solSo;

ola ile , . F ola ile bool. T

_hudtp solSo

,
_hudtp solSo

(C# R

) 3. , ,
1/5

T
_hudtp solSo

m dn.mic o of .com/en-

/lib a /7a2f3a 4(d=p in e , = .80).a p

17/01/12

Ho

o: C ea e and Te mina e Th ead (C#)

_hudtp I , solSo.

, . C ,

, , . T

. W . B T
4. T

Mi an

Wre okr Wre.ook okrDWr

Th ead C# Wre wreOjc okr okrbet Tra wreTra hed okrhed nwWre(; e okr) nwTra(okrbetDWr) e hedwreOjc.ook;

, . T C# wreTra.tr(; okrhedSat)
Mi an

, S
5

A . T
6 Mi an

, . T , ' I A C# wie(wreTra.slv) hl !okrhedIAie; e:


Mi an Mi an

, :
DWr ook

7. T

'
Mi an

DWr ook

C# Tra.le() hedSep1;

1
Wre.eusSo okrRqettp

, Mi an

C# wreOjc.eusSo(; okrbetRqettp)

I
m dn.mic o of .com/en/lib a /7a2f3a 4(d=p in e , = .80).a p

8,

2/5

17/01/12

Ho

o: C ea e and Te mina e Th ead (C#)

. T F T C# wreTra.on) okrhedJi(; ,
Mi an

. . T . , :

J , ,

Join

A , T . .

Mi an

. I

E ample
C# uigSse; sn ytm uigSse.hedn; sn ytmTraig pbi casWre ulc ls okr { / Ti mto wl b cle we tetra i satd / hs ehd il e ald hn h hed s tre. pbi vi DWr( ulc od ook) { wie(_hudtp hl !solSo) { CnoeWieie"okrtra:wrig."; osl.rtLn(wre hed okn..) } CnoeWieie"okrtra:triaiggaeul.) osl.rtLn(wre hed emntn rcfly"; } pbi vi Rqettp) ulc od eusSo( { _hudtp tu; solSo re } / Vltl i ue a hn t tecmie ta ti dt / oaie s sd s it o h oplr ht hs aa / mme wl b acse b mlil tras / ebr il e cesd y utpe hed. piaevltl bo _hudtp rvt oaie ol solSo; } pbi casWreTraEape ulc ls okrhedxml { sai vi Mi( ttc od an) { / Cet tetra ojc.Ti de ntsattetra. / rae h hed bet hs os o tr h hed Wre wreOjc okr okrbet nwWre(; e okr) Tra wreTra hed okrhed nwTra(okrbetDWr) e hedwreOjc.ook; / Sattewre tra. / tr h okr hed wreTra.tr(; okrhedSat) CnoeWieie"antra:Satn wre tra..) osl.rtLn(mi hed trig okr hed."; / Lo utlwre tra atvts / op ni okr hed ciae. wie(wreTra.slv) hl !okrhedIAie; / Pttemi tra t sepfr1mlieodt / u h an hed o le o ilscn o / alwtewre tra t d sm wr: / lo h okr hed o o oe ok Tra.le() hedSep1; / Rqetta tewre tra so isl: / eus ht h okr hed tp tef
m dn.mic o of .com/en/lib a /7a2f3a 4(d=p in e , = .80).a p 3/5

17/01/12

Ho

o: C ea e and Te mina e Th ead (C#)

wreOjc.eusSo(; okrbetRqettp) / UeteJi mto t boktecrettra / s h on ehd o lc h urn hed / utlteojc' tra triae. / ni h bets hed emnts wreTra.on) okrhedJi(; CnoeWieie"antra:Wre tra hstriae.) osl.rtLn(mi hed okr hed a emntd";

Sample Output

mi tra:satn wre tra.. an hed trig okr hed. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:wrig. okr hed okn.. wre tra:triaiggaeul.. okr hed emntn rcfl . mi tra:wre tra hstriae an hed okr hed a emntd

See Also
Tasks T Reference T U (C# P T (C# R T 12 M 13 M 14 S 6 I A 15 S J 16 8 A Concepts C# P Other Resources M T T S
1 18 4

10

G (C# P )3

)11 G

)2

17

Links Table
m dn.mic o of .com/en/lib a /7a2f3a 4(d=p in e , = .80).a p 4/5

You might also like