vb.net如何让两个窗体并发运行
vb.net以下程序如何做到:运行时移动form1窗体不影响form2窗体的显示。以下程序结果为移动Form1窗体时,form2窗体Label将停止显示。谢谢!Impor...
vb.net以下程序如何做到:运行时移动form1窗体不影响form2窗体的显示。以下程序结果为移动Form1窗体时,form2窗体Label将停止显示。谢谢!
Imports System.Threading
Public Class Form1
Dim t1, t2 As Thread
Dim tt1
Private Delegate Sub voiddelegate()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf test)
tt1 = 1
t1.Start()
tt1 = 2
t2 = New Thread(AddressOf test)
t2.Start()
End Sub
Private Sub updateui()
For i = 0 To 9000
Application.DoEvents()
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
Next
End Sub
Private Sub test()
Me.Invoke(New voiddelegate(AddressOf updateui))
End Sub
End Class 展开
Imports System.Threading
Public Class Form1
Dim t1, t2 As Thread
Dim tt1
Private Delegate Sub voiddelegate()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf test)
tt1 = 1
t1.Start()
tt1 = 2
t2 = New Thread(AddressOf test)
t2.Start()
End Sub
Private Sub updateui()
For i = 0 To 9000
Application.DoEvents()
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
Next
End Sub
Private Sub test()
Me.Invoke(New voiddelegate(AddressOf updateui))
End Sub
End Class 展开
1个回答
展开全部
Imports System.Threading
Public Class Form1
Dim t1, t2 As Thread
Dim tt1
Private Delegate Sub voiddelegate(ByVal i As Integer)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf test)
tt1 = 1
t1.Start()
tt1 = 2
t2 = New Thread(AddressOf test)
t2.Start()
' Timer1.Start()
End Sub
Private Sub updateui(ByVal i As Integer)
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
'把循环放到线程中运行会好一些,其实主要事务是对窗体控件的操作时多线程意义不大,因为访问窗体的代码依然要在窗体线程中运行。
Private Sub test()
Dim d As voiddelegate = AddressOf updateui
For i = 0 To 9000
Me.Invoke(d, i)
Next
End Sub
'如果放在计时器中运行效果好的多
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer = 0
i += 1
If i > 9000 Then Timer1.Stop() : i = 0
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
End Class
'如果在另一线程中用Application.run 打开窗体确实能行,不过窗体间相互操作不起作用,这个尚待研究中。
Public Class Form1
Dim t1, t2 As Thread
Dim tt1
Private Delegate Sub voiddelegate(ByVal i As Integer)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf test)
tt1 = 1
t1.Start()
tt1 = 2
t2 = New Thread(AddressOf test)
t2.Start()
' Timer1.Start()
End Sub
Private Sub updateui(ByVal i As Integer)
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
'把循环放到线程中运行会好一些,其实主要事务是对窗体控件的操作时多线程意义不大,因为访问窗体的代码依然要在窗体线程中运行。
Private Sub test()
Dim d As voiddelegate = AddressOf updateui
For i = 0 To 9000
Me.Invoke(d, i)
Next
End Sub
'如果放在计时器中运行效果好的多
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer = 0
i += 1
If i > 9000 Then Timer1.Stop() : i = 0
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
End Class
'如果在另一线程中用Application.run 打开窗体确实能行,不过窗体间相互操作不起作用,这个尚待研究中。
追问
非常感谢你的回答!
但是还是没能满足我要求的结果,我试了,当窗体form1中有程序段在运行的时候,timer1还是停止的。
其实我想做到的结果是,我程序在运行的时候,form2窗体作为动态提示信息,表示程序在运行没有死机(或提示运行过程中得到的一些信息),当form1窗体计算程序运行结束后自动退出form2窗体。我QQ:38794003,若能指教不胜感激!谢谢!
追答
你把程序发过来,2978454279@qq.com ,前面号码也是qq号,晚上研究了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询