Lm th? no ? t?o ch? ? trong Visual Basic.NET ho?c Visual Basic 2005

D?ch tiu ? D?ch tiu ?
ID c?a bi: 315577 - 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

Bi vi?t ny m t? cch t?o ch? ? trong m?t Microsoft Visual Basic.NET ho?c Microsoft Visual Basic 2005 Windows ?ng d?ng. Cc System.Threading khng gian tn cung c?p cc l?p h?c m b?n ph?i c ? lm vi?c v?i ch? ?.

Yu c?u

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

T?o m?t Visual Basic.NET ho?c Visual Basic 2005 ?ng d?ng v?i ch? ?

  1. B?t ?u Microsoft Visual Studio.NET ho?c Microsoft Visual Studio 2005.
  2. T?o m?t d? n ?ng d?ng Windows Visual Basic m?i tn l ThreadWinApp.
  3. Thm m?t Nt ki?m sot m?u. Cc nt ?c ?t tn theo Button1 Theo m?c ?nh.
  4. Thm m?t ProgressBar thnh ph?n m?u. Thanh tien tr?nh ?c ?t tn theo ProgressBar1 Theo m?c ?nh.
  5. Nh?p chu?t ph?i vo bi?u m?u v b?m Xem m?.
  6. Thm cc tuyn b? sau vo ?u c?a t?p tin:
    Imports System.Threading
    					
  7. Thm d?i y Nh?p vo x? l? s? ki?n cho Button1:
    Private Sub Button1_Click( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
    
        MessageBox.Show("This is the main thread")
    End Sub
    					
  8. Thm bi?n sau y ? cc Form1 l?p:
    Private trd As Thread
    					
  9. Thm cc phng php sau y ? cc Form1 l?p:
    Private Sub ThreadTask()
        Dim stp As Integer
        Dim newval As Integer
        Dim rnd As New Random()
    
        Do
            stp = ProgressBar1.Step * rnd.Next(-1, 2)
            newval = ProgressBar1.Value + stp
            If newval > ProgressBar1.Maximum Then
                newval = ProgressBar1.Maximum
            ElseIf newval < ProgressBar1.Minimum Then
                newval = ProgressBar1.Minimum
            End If
    
            ProgressBar1.Value = newval
    
            Thread.Sleep(100)
        Loop
    End Sub
    						
    LU ?: y l m? r?ng n?n t?ng ch? ?. M? ny l m?t v?ng l?p v h?n ng?u nhin gia s? ho?c decrements gi tr? trong ProgressBar1 v sau ch? ?i 100 mili giy tr?c khi 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 ch? ? m?i, lm cho s?i ch? m?t ch? ? n?n v sau b?t ?u thread.
    Private Sub Form1_Load( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load
    
        trd = New Thread(AddressOf ThreadTask)
        trd.IsBackground = True
        trd.Start()
    End Sub
    					

Xc minh r?ng n ho?t ?ng

  1. Xy d?ng v ch?y cc ?ng d?ng. Ch ? r?ng cc gi tr? trong thanh tien tr?nh thay ?i ng?u nhin. y l ch? ? m?i trong ho?t ?ng.
  2. ? ch?ng minh r?ng ch? ? chnh l ?c l?p v?i cc ch? ? thay ?i cc ProgressBar c gi tr?, nh?p vo bi?u m?u. M?t h?p thng i?p s? hi?n th? thng bo sau:
    This is the main thread
    							
    Ch? ?i cho ?u vo. Ch ? r?ng cc gi tr? trong thanh tien tr?nh ti?p t?c thay ?i.

Khc phuc s c

Trong cc ?ng d?ng ph?c t?p hn, h?y ch?c ch?n r?ng b?n ?ng b? ha ch? ? c?a nhi?u khi b?n truy c?p vo cc bi?n ?c chia s?. ? bi?t thm thng tin, tham kh?o cc SyncLock tuyn b? v cc ch? ? c lin quan trong ti li?u tr? gip tr?c tuy?n c?a Visual Basic.

Thu?c tnh

ID c?a bi: 315577 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
T? kha:
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB315577 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:315577

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