打开对话框时为什么计时器停止? [英] Why timer stops when a dialog box is opened?

查看:95
本文介绍了打开对话框时为什么计时器停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我在我的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.


谢谢



我删除了CheckForIllegalCrossThreadCalls = False



这里是其余代码:



Sub CountTraffic(byval var As Object)

_CountUnauthorized + = 1

End Sub



这是一个与线程一起工作的测试项目。



_CountUnauthorized是一个表单级变量。因为我删除了那行,我将值放在表单级别变量中,使用Timer对象读取变量并在Label中显示。但是我怎么能解决上面提到的问题呢。



抱歉,我是初学者...



谢谢
thank you

I removed "CheckForIllegalCrossThreadCalls = False"

here is rest of code:

Sub CountTraffic(byval var As Object)
_CountUnauthorized +=1
End Sub

this was a test project to work with thread.

"_CountUnauthorized" is a form level variable. because I removed that line I put the value in a form level variables and with Timer Object I read the variables and show in Label. but how can I solve of mentioned problem.

sorry I am beginner in thread...

thank you


这篇关于打开对话框时为什么计时器停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆