vb.net 创建窗口句柄时出错,Timer 中出现问题
ImportsSystem.Timers.TimerImportsSystem.TimersPublicClassForm1Privatetimer1AsSystem.T...
Imports System.Timers.Timer
Imports System.Timers
Public Class Form1
Private timer1 As System.Timers.Timer
Private timer2 As System.Timers.Timer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Dim j As Integer
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
timer1 = New System.Timers.Timer(100)
timer2 = New System.Timers.Timer(100)
AddHandler timer1.Elapsed, AddressOf Ontimer1Event
AddHandler timer2.Elapsed, AddressOf Ontimer2Event
timer1.Enabled = True
timer2.Enabled = True
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub Ontimer1Event(ByVal source As Object, ByVal e As ElapsedEventArgs)
j = j + 1
Label1.Text = j
End Sub
Private Sub Ontimer2Event(ByVal source As Object, ByVal e As ElapsedEventArgs)
i = i + 100
Label2.Text = i
End Sub
End Class
用两个定时器,分别对 各自对应的label.Text 赋值, 编译运行时都没错,但是点击应用程序右上角的关闭按钮,会弹出 用户代码未处理 Win32Exception
创建窗口句柄时出错。
why? 求解
timer2.Enabled= False 时, 运行没有弹出上述错误。
也就是不能两个定时器同时对UI 控件访问。 一个定时器时,没问题,我已经把Control.CheckForIllegalCrossThreadCalls = False 展开
Imports System.Timers
Public Class Form1
Private timer1 As System.Timers.Timer
Private timer2 As System.Timers.Timer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Dim j As Integer
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
timer1 = New System.Timers.Timer(100)
timer2 = New System.Timers.Timer(100)
AddHandler timer1.Elapsed, AddressOf Ontimer1Event
AddHandler timer2.Elapsed, AddressOf Ontimer2Event
timer1.Enabled = True
timer2.Enabled = True
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub Ontimer1Event(ByVal source As Object, ByVal e As ElapsedEventArgs)
j = j + 1
Label1.Text = j
End Sub
Private Sub Ontimer2Event(ByVal source As Object, ByVal e As ElapsedEventArgs)
i = i + 100
Label2.Text = i
End Sub
End Class
用两个定时器,分别对 各自对应的label.Text 赋值, 编译运行时都没错,但是点击应用程序右上角的关闭按钮,会弹出 用户代码未处理 Win32Exception
创建窗口句柄时出错。
why? 求解
timer2.Enabled= False 时, 运行没有弹出上述错误。
也就是不能两个定时器同时对UI 控件访问。 一个定时器时,没问题,我已经把Control.CheckForIllegalCrossThreadCalls = False 展开
1个回答
展开全部
因为你窗口关闭的时候Time并没有释放,但是此时Label已经不能访问了
才会出现窗口句柄出错,你试试加上下面的话
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
timer1.Close()
timer2.Close()
End Sub
应该就不会报错了。
希望能帮到你。
才会出现窗口句柄出错,你试试加上下面的话
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
timer1.Close()
timer2.Close()
End Sub
应该就不会报错了。
希望能帮到你。
更多追问追答
追问
试过了,加上 FormClosing 可以, 没问题。
但是我还有个问题,为什么把第二个timer2.Enabled 设为false,只让一个timer运行时, 关闭应用程序时,没有弹出异常。
追答
呵呵,这个就是关闭窗体的瞬间time2的事件仍在执行。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询