vb.net 多线程调用另一窗口,假死现象,如何解决
'窗体1ImportsSystemImportsSystem.ThreadingPublicClassForm1PublicSubStartTimer()DimtcbAs...
'窗体1
Imports System
Imports System.Threading
Public Class Form1
Public Sub StartTimer()
Dim tcb As New TimerCallback(AddressOf Me.TimerMethod)
Dim objTimer As Timer
objTimer = New Timer(tcb, Nothing, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10))
End Sub
Public Sub TimerMethod(ByVal state As Object)
'处理其它事物的程序略
Form2.Show() '启动倒计时显示窗口
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartTimer()
End Sub
End Class
'窗体2(到计时器)
Public Class Form2
Dim s As Integer = 60
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = s
s -= 1
If s = 0 Then
Me.Close()
End If
End Sub
End Class 展开
Imports System
Imports System.Threading
Public Class Form1
Public Sub StartTimer()
Dim tcb As New TimerCallback(AddressOf Me.TimerMethod)
Dim objTimer As Timer
objTimer = New Timer(tcb, Nothing, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10))
End Sub
Public Sub TimerMethod(ByVal state As Object)
'处理其它事物的程序略
Form2.Show() '启动倒计时显示窗口
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartTimer()
End Sub
End Class
'窗体2(到计时器)
Public Class Form2
Dim s As Integer = 60
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = s
s -= 1
If s = 0 Then
Me.Close()
End If
End Sub
End Class 展开
展开全部
把Form2.Show()改成:
BeginInvoke(Sub() Form2.Show())
BeginInvoke(Sub() Form2.Show())
追问
谢谢,不会假死了,但Form2不能每10秒弹一个倒计时器,而是这个form2自动关闭后10秒才再次弹出。能否每10秒弹一个Form2出来?
追答
建议你用System.Windows.Forms里面的Timer而不是System.Threading里面的,这个很复杂而且线程不安全
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询