问题描述
你好
我在我的MainForm中有这个代码:
CheckForIllegalCrossThreadCalls = False
Dim CountTrafficThread As New Threading.Timer(New Threading.TimerCallback(AddressOf CountTraffic),Nothing,0,500)
它运行良好但是,当我从这个表单调用一个对话框时,计时器停止,计数器不能计数。如果我使用工具箱中的计时器对象,即使在调用对话框后也能正常工作。
请帮帮我。
谢谢
我尝试过:
在网上搜索找到解决方案
hello
I have this code in my MainForm:
CheckForIllegalCrossThreadCalls = False
Dim CountTrafficThread As New Threading.Timer(New Threading.TimerCallback( AddressOf CountTraffic), Nothing, 0, 500)
it works good but, when I call a dialog box from this form, the timer is stopped and counter can not count. If i use a Timer Object from toolbox it work good even after calling dialog box.
please help me.
thank you
What I have tried:
Just search on the web to find solution
推荐答案
CheckForIllegalCrossThreadCalls = False
删除代码行,忘了它甚至存在。你正在设置很难复制和修复的bug。
计时器不会停止。它还在运行。问题是你的代码具有被对话框阻止定时器事件被触发的线程,所以线程在对话框被解除之前不能做任何事情。
你是如何解决的?不知道。我们只能看到两行代码,而且我们对代码的设计一无所知。但是,鉴于您放入了CheckForIllegalCrossThreadCalls行,您的代码可能需要重写才能正确执行操作,而不是修补废话以强制它以错误的方式工作。
Remove that line of code and forget it even exists. You're setting yourself up for bugs that are very difficult to replicate and fix.
The timer doesn't stop. It's still running. The problem is your code has the thread that the timer event is firing on blocked with the dialog so the thread can't do anything else until the dialog is dismissed.
How do you fix it? Don't know. We can only see two lines of code and we know nothing of the design of your code. But, given that you put in the CheckForIllegalCrossThreadCalls line, your code is probably going to need to be rewritten to do things correctly instead of patching crap together to force it to work the wrong way.
这篇关于打开对话框时为什么计时器停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!