Lm th? no ? t?o ra m?t ch? ? b?ng cch s? d?ng Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 815804 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

B?n c th? vi?t multithreaded ?ng d?ng trong Microsoft Visual C#.NET hay trong Microsoft Visual C#. Bi vi?t ny m t? lm th? no m?t n gi?n Visual C# n c th? t?o v qu?n l? ch? ?.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, h? t?ng m?ng, v gi d?ch v? m b?n c?n:
  • Microsoft Windows 2000, Microsoft Windows XP ho?c Microsoft Windows Server 2003
  • Microsoft Visual C#.NET ho?c Microsoft Visual C# 2005
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i sau cc ch? ?:
  • Visual C# l?p tr?nh
  • Visual Studio.L?I mi tr?ng pht tri?n tch h?p (IDE) ho?c Visual Studio 2005 IDE

T?o ?ng d?ng Visual C# v?i ch? ?

  1. B?t ?u Microsoft Visual Studio.NET, Microsoft Visual Studio 2005 ho?c Microsoft Visual C# 2005 Express Edition.
  2. T?o m?t d? n m?i Visual C# Windows ?ng d?ng ?t tn ThreadWinApp.
  3. Thm m?t Nt ki?m sot m?u. B?i m?c ?nh, cc nt ?c ?t tn theo Button1.
  4. Thm m?t ProgressBar thnh ph?n m?u. Theo m?c ?nh, thanh ti?n ? ?c ?t tn theo ProgressBar1.
  5. Nh?p chu?t ph?i vo bi?u m?u v b?m Xem Ma.
  6. Thm cc tuyn b? sau vo ?u c?a t?p tin:
    using System.Threading;
  7. Thm d?i y Nh?p vo x? l? s? ki?n choButton1:
    private void button1_Click(object sender, System.EventArgs e)
    {
    	MessageBox.Show("This is the main thread");
    }
  8. Thm bi?n sau y ? cc Form1 l?p:
    private Thread trd;
    
  9. Thm cc phng php sau y ? cc Form1 l?p:
    private void ThreadTask()
    {
    	int stp;
    	int newval;
    	Random rnd=new Random();
    
    	while(true)
    	{
    		stp=this.progressBar1.Step*rnd.Next(-1,2);
    		newval = this.progressBar1.Value + stp;
    
    		if (newval > this.progressBar1.Maximum)
    			newval = this.progressBar1.Maximum;
    		else if (newval < this.progressBar1.Minimum)
    			newval = this.progressBar1.Minimum;
    		
    		this.progressBar1.Value = newval;
    
    		Thread.Sleep(100);
    	}
    }
    Chu y y l m? r?ng n?n t?ng ch? ?. M? ny l m?t v h?n l?p ng?u nhin s? gia hay decrements gi tr? trongProgressBar1, v sau ch? ?i 100 mili giy tr?c khi n ti?p t?c.
  10. Thm d?i y T?i x? l? s? ki?n cho Form1. M? ny t?o ra m?t m?i ch? ?, lm cho s?i ch? m?t ch? ? n?n, v sau b?t ?u thread.
    private void Form1_Load(object sender, System.EventArgs e)
    {
    	Thread trd = new Thread(new ThreadStart(this.ThreadTask));
    	trd.IsBackground = true;
    	trd.Start();
    }

Xc minh r?ng n ho?t ?ng

  1. Xy d?ng v ch?y cc ?ng d?ng. Nh?n th?y r?ng cc gi tr? trongProgressBar1 thay ?i ng?u nhin. y l ch? ? m?i trong thao tc.
  2. ? ch?ng minh r?ng ch? ? chnh l ?c l?p v?i cc ch? ? thay ?i gi tr? c?a ProgressBar1, b?m vo cc nt trn bi?u m?u. B?n nh?n ?c m?t h?p tho?i v?i thng bo l?i sau:
    This is the main thread
Ch? ?i cho ?u vo. Nh?n th?y r?ng cc gi tr? trong ProgressBar1 ti?p t?c thay ?i.

G? r?i

Trong cc ?ng d?ng ph?c t?p hn, h?y ch?c ch?n r?ng b?n ?ng b? ha nhi?u ch? ? khi b?n truy c?p vo cc bi?n ?c chia s?. ? bi?t thm chi ti?t, xem cc kha tuyn b? v cc ch? ? c lin quan trong cc Visual C#.NET Ti li?u tr? gip tr?c tuy?n.

THAM KH?O

? bi?t thm chi ti?t, gh thm Web site sau c?a Microsoft ho?c cc.NET Framework SDK ti li?u:
"Thread L?p h?c"
http://msdn2.Microsoft.com/en-us/Library/System.Threading.thread (vs.71) .aspx

Thu?c tnh

ID c?a bi: 815804 - L?n xem xt sau cng: 23 Thang Chin 2011 - Xem xt l?i: 3.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
T? kha:
kbnamespace kbthreadsync kbthread kbhowtomaster kbmt KB815804 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:815804

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com