C# 如何从不同的线程/类启用计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/725735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable a timer from a different thread/class
提问by Ivan Prodanov
Original post: How to access a timer from another class in C#
I tried everything.
我尝试了一切。
-Event
-事件
-Invoke can't be done,because Timers don't have InvokeRequired property.
-Invoke 无法完成,因为 Timers 没有 InvokeRequired 属性。
-Public/internal property
-公共/内部财产
Nothing worked,the code is being executed,the timer.Enabled property is being set to "true" ,but it doesn't Tick.If I call the event or just change the property from the form class in a NON-static method - it does tick and works.
没有任何效果,正在执行代码,timer.Enabled 属性被设置为“true”,但它没有勾选。如果我调用事件或只是在非静态方法中更改表单类中的属性 -它确实打勾并且有效。
I never knew it would take me a day and probably more to acquire how to use a decent timer.
我从来不知道我需要一天甚至更多的时间才能学会如何使用一个像样的计时器。
If there's no way to do this,is there anything else I can use that works similiar to the timer(delay,enable/disable)?
如果没有办法做到这一点,还有什么我可以使用的类似于计时器的工作(延迟,启用/禁用)?
采纳答案by David M
You should use the Timer class from the System.Timers namespace, and not the WinForms Timer control, if you want multi-threading support. Check the MSDN documentation for the WinForms Timer control for more information:
如果您想要多线程支持,您应该使用 System.Timers 命名空间中的 Timer 类,而不是 WinForms Timer 控件。有关详细信息,请查看 WinForms Timer 控件的 MSDN 文档:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
回答by Ivan Prodanov
I accidently tried to use invoke again,this time it worked,but I'll accept your answer,DavidM.
我不小心再次尝试使用 invoke,这次成功了,但我会接受你的回答,DavidM。
public bool TimerEnable
{
set
{
this.Invoke((MethodInvoker)delegate
{
this.timer.Enabled = value;
});
}
}
public static void timerEnable()
{
var form = Form.ActiveForm as Form1;
if (form != null)
form.TimerEnable = true;
}
回答by Samuel
Just because System.Windows.Forms.Timerdoesn't have the ability to invoke, doesn't mean that your form doesn't. Try my InvokeExfrom the second (or other) thread to enable the timer.
仅仅因为System.Windows.Forms.Timer没有调用能力,并不意味着你的表单没有。InvokeEx从第二个(或其他)线程尝试 my以启用计时器。
public static class ControlExtensions
{
public static TResult InvokeEx<TControl, TResult>(this TControl control,
Func<TControl, TResult> func)
where TControl : Control
{
if (control.InvokeRequired)
{
return (TResult)control.Invoke(func, control);
}
else
{
return func(control);
}
}
}
With this, the following code worked for me:
有了这个,以下代码对我有用:
new Thread(() =>
{
Thread.Sleep(1000);
this.InvokeEx(f => f.timer1.Enabled = true);
}).Start();
And the timer instantly sprung to life after the 1 second.
计时器在 1 秒后立即启动。
回答by Seibar
You don't need to check InvokeRequiredon the Control iteself, you can check the property on the class e.g.:
您不需要检查InvokeRequiredControl 本身,您可以检查类上的属性,例如:
if (this.InvokeRequired)
{
BeginInvoke(new MyDelegate(delegate()
{
timer.Enabled = true;
}));
}

