读书人

窗体起步的时候自动循环一条线程可是

发布时间: 2013-04-02 12:35:26 作者: rapoo

窗体启动的时候自动循环一条线程,可是输不值到外面

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf abc)
Thread1.Start()
Label2.Text = j

'Thread1.Join()
End Sub

Public Sub abc()
Do
If i Then
j += 1
If j = 500000 Then j = 0
Application.DoEvents()
End If
Loop

End Sub

我要实现的功能:窗体启动的时候,不停地执行线程1:线程1里面有一个判断语句,If i Then,根据外面的按钮改变i的 Boolean值,控制循环,同时当i=true时,执行j+=1,并且在窗体中显示i,j的当时值,奇怪的时候,J的值输出不了到窗体中。求方法求解析求帮助~
[解决办法]
给分吧



Public Class Form1
Private i As Boolean = True, j As Integer = 0
Public Delegate Sub DelegateSub(ByVal text As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
i = Not i
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Thread1 As New System.Threading.Thread(AddressOf abc)
Thread1.Start()
End Sub

Private Sub SetText(ByVal value As String)
Label2.Text = value
End Sub
Public Sub abc()
Do
If i Then
j += 1
If j = 500000 Then j = 0
Invoke(New DelegateSub(AddressOf SetText), j.ToString)
Application.DoEvents()
End If
Loop

End Sub
End Class



读书人网 >VB Dotnet

热点推荐