How to create threads in Visual Basic .NET or Visual Basic 2005

???? ?????? ???? ??????
???? ID: 315577 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

This article describes how to create threads in a Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 Windows application. TheSystem.Threadingnamespace provides the classes that you must have to work with threads.

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows 2000 ?? Microsoft Windows XP
  • Microsoft Visual Basic .NET ?? Microsoft Visual Basic 2005
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • Visual Basic .NET programming or Visual Basic 2005 programming
  • Visual Studio .NET or Visual Studio 2005 Integrated Development Environment (IDE)
  • ?? ?? ???????????? ?? ??? ??????? familiarity
  • Visual Basic .NET ?? Visual Basic 2005 ?? ??? ??????? familiarity

????? ?? ??? ??? Visual Basic .NET ?? Visual Basic 2005 ????????? ?????

  1. Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005 ??????? ?????
  2. ??? ?? Windows ????????? ?? Visual Basic ????????? ThreadWinApp ??? ??????
  3. ????? ??????????????? ???? ?? ??? ???????? ??? ?? ???Button1???????? ??? ???
  4. ????? ???ProgressBar??????? ?? ??? ???? ?????? ????? ?? ???ProgressBar1???????? ??? ???
  5. ??????? ?? ????-????? ????, ?? ???? ??? ????? ????????? ???.
  6. ????? ??? ????? ?? ???? ??? ??????:
    Imports System.Threading
    					
  7. ????? ??????????? ????,?? ??? ????? ??????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. ???? ?? ??? ????? ?? ??????Form1????:
    Private trd As Thread
    					
  9. ???? ?? ??? ????? ???? ??????Form1????:
    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
    						
    ???:: ?? ????? underlies ??? ??? ?? ??? ?? ?????????? increments ?? infinite ??? ?? decrements ??? ???ProgressBar1?? ????????? ?? ???? 100 ????????? ???? ?? ???? ?? ??? ???? ???
  10. ????? ????????? ?????? ??? ????? ??????Form1. ?? ??? ?? ????? ????? ??, ????????? ????? ????? ????? ?? ?? ?? ????? ??????? ?? ???? ???
    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
    					

???????? ???? ?? ??? ???

  1. ?????, ?? ????????? ?????? ????? ??? ?? ?????? ????? ??? ??? ????????? ?????????? ??? ?? ???????? ??? ?? ????? ???
  2. ????? ????? ????? ?? ?? ????? ?? ??? ???????? ?? demonstrate ???? ?? ???ProgressBar???, ??????? ?? ??? ????? ????? ??? ????? ????? ????? ????? ????????? ???? ??:
    This is the main thread
    							
    ????? ?? ????????? ?? ??? ??? ????? ??? ?? ?????? ????? ??? ??? ????????? ???? ?? ??? ??? ???? ???

?????? ??????

In more complex applications, make sure that you synchronize multiple threads when you access shared variables. For more information, refer to theSyncLockstatement and related topics in the Visual Basic Online Help documentation.

???

???? ID: 315577 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB315577 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:315577

??????????? ???

 

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