【发布时间】:2017-01-11 14:59:01
【问题描述】:
我有一个应用程序,其中我正在初始化一个Windows Form 和一个System.Threading.Timer 的类。
如果timer(不断检查某些基于IPC 的内容)遇到特定值,它会发出同一类中的event 信号,然后在之前初始化的对话框中调用ShowDialog()。
不幸的是,这个ShowDialog() 是Modal,停止了计时器。
我的印象是System.Threaded.Timer 是在与调用线程不同的线程中创建的,因此 Timer 将继续在后台运行。
编辑 - 一些代码
public delegate void EventHandler();
class someClass
{
WrapperForm dlg = null;
public void CallToChildThread(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
//Check IPC
//Fire event
_show.Invoke();
}
public someClass()
{
public static event EventHandler _show;
initializeDialog(); // Initialize the dialog. Standard new
var autoEvent = new AutoResetEvent(false);
var stateTimer = new System.Threading.Timer(CallToChildThread,
autoEvent, 1000, 250);
_show += new EventHandler(eventCheck);
}
void eventCheck()
{
//If some condition
dlg.ShowDialog(); //Timer stops
}
}
如何解决?
【问题讨论】:
-
如果你能让我们看看你尝试了什么(一些代码),那就太好了。
-
模式不会为我暂停计时器 o.O
-
我做了一些更改,以便反映您的代码,希望对您有所帮助。
标签: c# multithreading winforms timer