从Timer.Tick调用的方法阻止了GUI线程 [英] GUI thread is blocked by method invoked from Timer.Tick

查看:67
本文介绍了从Timer.Tick调用的方法阻止了GUI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 System.Windows.Forms.Timer 的事件中重复运行一个方法。在那种方法中,我将不得不等待一段时间。我如何管理GUI在我正在等待计时器事件执行的方法时不会阻塞?



谢谢!



[OP] 评论:我希望我能正确翻译你的问题。

i希望循环运行一些基于UI线程的方法,所以我使用System.Windows.Forms.Timer创建一个计时器thead。

但是我必须等待的计时器thead一段时间。

所以,我怎么做,基于UI线程,但不要等待一段时间。

任何人都可以给我一个演示!

谢谢。

[/ OP]





I want to repeatedly run a method in an event of a System.Windows.Forms.Timer. In that method I will have to wait for a certain amount of time. How can I manage it that the GUI won't block for the time I'm waiting in the method executed by the timer's event?

Thank you!

[OP] Comment: I hope I correctly translated your problem.
i want to loop run some method based on UI thread,so i use System.Windows.Forms.Timer to create a timer thead.
but the timer thead that i must be wait for a while.
so, how would i do,based on UI thread,but not wait for a while.
can anybody give me a demo!
thanks.
[/OP]


System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer();
tm.Tick += new EventHandler(tm_Tick);
tm.Interval = 20000;

 void tm_Tick(object sender, EventArgs e)
 {
      DaliyTask();
            
 }





DaliyTask方法必须基于UI线程,但是tm线程需要等待20s,我不想等20秒,所以我怎么能解决这个问题。



the DaliyTask method must based on UI thread,but tm thread need to wait 20s,i don't want to wait 20s,so how can i solve this problem.

推荐答案

你好xuyunhai,



如果你不想通过一些冗长的计算来阻止你的GUI线程,或者只是 Thread.Sleep()在由计时器调用的方法中.Tick 事件,您必须将该计算/处理移动到另一个线程。你可以在这里开始阅读(用简单的例子):线程教程C# [ ^ ]。



[更新]



感谢您发布代码示例。遗憾的是,没有办法在GUI线程上执行DailyTask方法而无需等待。很可能你想在GUI线程上轻松访问表单的控件。如果您需要从不同于GUI线程的线程中访问表单的控件,您可以使用 Control.Invoke(...)方法: Control.Invoke方法(委托) [ ^ ]。



[/ Update]



问候,



- Manfred
Hi xuyunhai,

if you don't want to block your GUI thread by some lengthy calculation or just Thread.Sleep() in a method invoked by a Timer.Tick event, you will have to move that calculation/processing to a different thread. You can start out reading here (with simple examples): Threading Tutorial C#[^].

[Update]

Thanks for posting the code sample. The sad story is there is no way to do the DailyTask method on the GUI thread and not having to wait. Most probably you want it on the GUI thread to have easy access to the form's controls. If you need to access the form's controls from within a thread different from the GUI thread you can use the Control.Invoke(...) method: Control.Invoke Method (Delegate)[^].

[/Update]

Regards,

— Manfred


对于我在触发事件发生后立即进入线程的想法没有任何意义,但可以这样做:



创建一个新线程和一个 AutoresetEvent (一种信号量)最初未发出信号( false )。在线程的方法创建一个循环,首先使用 AutoresetEvent 中的WaitOne()方法等待,然后调用 Thread.Sleep()并继续工作。



添加一个计时器并根据需要配置它。在计时器的 Tick 事件处理程序中调用 AutoresetEvent 的Set()方法来唤醒线程。



关闭应用程序时,请注意实现一种关闭线程的方法。另外还要考虑到你无法从线程的方法直接访问 GUI
It makes no sense for me your idea of putting to sleep the thread just after the triggering event, but it could be done this way:

Create one new thread and one AutoresetEvent (kind of a semaphore) initially not signalled (false). On the thread's method create one loop that first waits using the method WaitOne() from the AutoresetEvent and, after that, call Thread.Sleep() and continue with your work.

Add one timer and configure it for the interval you need. On the timer's Tick event handler call the AutoresetEvent's Set() method to wake up the thread.

Take care of implementing one method to close the thread when closing the application. Also take into account that you can not directly access the GUI from the thread's method.


这篇关于从Timer.Tick调用的方法阻止了GUI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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