Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]


C#
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();
            
 }


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.
Posted
Updated 26-Sep-13 23:47pm
v4
Comments
CPallini 27-Sep-13 5:06am    
?
xuyunhai 27-Sep-13 5:07am    
what?do not understand my meaning?
Pheonyx 27-Sep-13 5:15am    
Your question is unclear and quite confusing, could you use the "Improve Question" facility and try and further explain what you are trying to achieve and what you have tried so far. Where appropriate use formatted code snippets to help with your explanation and question. There are formatting tools built into this site which you can use to correctly display snippets of code etc.
Manfred Rudolf Bihy 27-Sep-13 6:00am    
I updated my solution according to you code sample. The bad part is you can't have DailyTask operate on the GUI thread if it involves waiting for a certain amount of time without blocking your GUI thread. See my solution for details to achieve what I think you want.

The question that comes to mind is why would you need DailyTask to run on the GUI thread? If access to the form's controls is the answer my solution will show you how to solve it.

Cheers!
Manfred Rudolf Bihy 27-Sep-13 5:24am    
Hi xuyunhai,
I tried to reformulate your question in order to make it better understandable. Please check if what I've transscripted is what you meant to say. Also I'd advise you to add some code that illustrates your problem. Try to construct code that will just be enough to illustrate your problem. There is little appreciation if you dump pages and pages of code. Make a concise small example that will show us what you're trying to do. For example: If waiting in the method called by the event means some convoluted calculation, we don't want to see it. The waiting can then just be expressed by Thread.Sleep() call with maybe a comment that the Tread.Sleep() is a placeholder for some calculation.

Regards,
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
 
Share this answer
 
v3
Comments
CPallini 27-Sep-13 5:42am    
5.And another (virtual) 5 for the OP question translation.
Manfred Rudolf Bihy 27-Sep-13 5:45am    
Thanks! :D
xuyunhai 27-Sep-13 6:00am    
I updated my question,thanks to read again.
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.
 
Share this answer
 
Comments
xuyunhai 27-Sep-13 6:00am    
I updated my question,thanks to read again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900