16,549
社区成员
发帖
与我相关
我的任务
分享
Imports System
Imports System.Threading
Imports System.Text
Public Class Form1
Private Delegate Sub voidDelegate()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckForIllegalCrossThreadCalls = False '不写这行,会出错,不允许线程的数据写到TextBox1.Text 中去。
Form2.Show()
End Sub
Private Sub form1_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing
' If runThread.IsAlive = True Then runThread.Abort()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'用的是委托方式
Me.Invoke(New voidDelegate(AddressOf THREAD2))
End Sub
Private Sub THREAD2()
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [线程] 操作" & vbCrLf & _
" Button2被点了: " & j & " 次" & vbCrLf & "要求是from2.textbox.text= textbox1.text 。[问题]但为什么不能成功显示呢?"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [非线程] 操作" & vbCrLf & _
"Button3 点击了: " & j & " 次" & vbCrLf & _
"要求是from2.textbox.text= textbox1.text, 可以成功显示,这个是对的。"
End Sub
End Class
Imports System
Imports System.Threading
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckForIllegalCrossThreadCalls = False '不写这行,会出错,不允许线程的数据写到TextBox1.Text 中去。
Form2.Show()
End Sub
Private Sub form1_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing
' If runThread.IsAlive = True Then runThread.Abort()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'用的是委托方式
Me.Invoke(New MethodInvoker(AddressOf THREAD2)) ‘这样更方便,不用delegate声明半句。
End Sub
Private Sub THREAD2()
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [线程] 操作" & vbCrLf & _
" Button2被点了: " & j & " 次" & vbCrLf & "要求是from2.textbox.text= textbox1.text 。[问题]但为什么不能成功显示呢?"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Static j As Integer
j = j + 1
TextBox1.Text = " 这是 [非线程] 操作" & vbCrLf & _
"Button3 点击了: " & j & " 次" & vbCrLf & _
"要求是from2.textbox.text= textbox1.text, 可以成功显示,这个是对的。"
End Sub
End Class