System.Threading.Timer不启动? [英] System.Threading.Timer Not Starting?

查看:345
本文介绍了System.Threading.Timer不启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#与Compact Framework的2,SP2。



该设备的操作系统被设置为与我的应用程序启动,让我们调用应用程序装载机。 。exe文件



装载机很简单:一个单一的,简单的形式,显示在整个负载状态信息,如果需要的话(通俗地说为那里是一个错误和异常消息,或者启动应用程序[某某]),和一个状态机在后台运行时显示一个基本的全屏幕形式。



所以装载机的形式的构造具有以下在非常结束:

 
{
label1.Text = 启动GUI初始化主题... //调试唯一的消息
System.Threading.Timer guiInit =新System.Threading.Timer(
RunStateMachine,空,2000年,System.Threading.Timeout.Infinite
);
//回调:RunStateMachine,空参数
//初始回调从这点2000毫秒,并且不会再次运行。
}
赶上(例外EX1)
{
label1.Text =GUI初始化错误2
Failure_Label.Text = ex1.Message;
}

和RunStateMachine不工作在不同的线程比UI,使表单中显示,任何时候RunStateMachine需要与形式的互动,比如更新的消息,我调用使用一个函数,如果(this.InvokeRequired){this.Invoke(...);}其他{...}



所以,我的问题?结果
间歇,我的程序将被挂起,这是因为定时器没有触发回调。我在调试信息添加在try块以上,其他很多地方一起告诉我在那里挂了,包括最开始的一个消息RunStateMachine。最终,我的程序挂在消息启动GUI初始化主题...



这告诉我,该线程定时器没有运行在有一个时间我需要它。结果
我的理论是,它被垃圾之前计时器触发回调收集。这将意味着如果计时器是全球性的,然后将其置于明确当我到RunStateMachine,它将完美运行...但我不想觉得我解决它,只是为了找到这来了间歇一个月从现在开始。



思考?


解决方案

我的理论是,它被垃圾之前计时器
触发回调收集。这将意味着如果计时器是全球性的,而且
,则显式处理,当我到达RunStateMachine,它将完全
运行...但我不想觉得我解决它只是为了找到
本间歇来了一个从现在起一个月。




它看起来像你想的确认,这是你的问题。 是,这就是问题所在。



定时器存储在其中永远不会再使用过的局部变量。这使得它符合GC。定时器的GC'ing导致最终确定导致禁用计时器。



我建议你保存计时器在你的窗体类的一个实例字段,将其取下从那里一旦回调已经解雇了。


I'm using C# with Compact Framework 2, SP2.

The device's OS was set up to start up with my application, let's call the application "Loader.exe."

Loader is simply this: a single, plain form that shows status messages throughout loading, if necessary (layman's terms for there being an error and an exception message, or "starting application [xyz]"), and a state machine running in the background while a basic full-screen form is shown.

So Loader's form's constructor has the following at the very end:

try
{
    label1.Text = "Starting GUI Init Thread...";  //debug only message
    System.Threading.Timer guiInit = new System.Threading.Timer(
        RunStateMachine, null, 2000, System.Threading.Timeout.Infinite
        );
    //callback: RunStateMachine, null argument
    //initial callback is 2000ms from this point, and doesn't run again.
}
catch (Exception ex1)
{
    label1.Text = "GUI Init Error 2";
    Failure_Label.Text = ex1.Message;
}

And "RunStateMachine" does work on a different thread than the UI, allowing the form to display, and any time RunStateMachine needs to interact with the form, such as updating messages, I call a function that uses if(this.InvokeRequired){this.Invoke(...);} else{...}

So, my problem?
Intermittently, my program will hang, and it's because the timer didn't trigger a callback. I added in the debug message in the try block above, along with MANY other places to tell me where it hung up, including a message at the VERY start of "RunStateMachine." Eventually, my program hung on the message "Starting GUI Init Thread..."

This tells me that the thread timer is not running the one time I need it to.
My theory is that it's being garbage collected prior to the timer triggering the callback. That would mean if the timer was global, and then disposed explicitly when I get to RunStateMachine, that it will run perfectly... but I don't want to think I solved it just to find this coming up intermittently a month from now.

Thoughts?

解决方案

My theory is that it's being garbage collected prior to the timer triggering the callback. That would mean if the timer was global, and then disposed explicitly when I get to RunStateMachine, that it will run perfectly... but I don't want to think I solved it just to find this coming up intermittently a month from now.

It looks like you want confirmation that this is your problem. Yes, this is the problem.

The timer is stored in a local variable which is never used ever again. This makes it eligible for GC. The GC'ing of the Timer leads to finalization leading to the timer to be disabled.

I suggest you store the timer in an instance field of your form class and remove it from there once the callback has fired.

这篇关于System.Threading.Timer不启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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