How can I wait for a given seconds without blocking.
-
Hi, /********** Visual Studio 2005, C# .NET 2.0 - Windows Form ***********/ Within my code, I want to hold for certain seconds like Thread.Sleep(2000). But I dont want to block the User Interfaces. Now, I had a choice to use Timer, and When I need to start sleeping, I can call the timer.Start() and use While Loop to check how many seconds passed, if time over then exit the loop, otherwise Application.DoEvents(). But This is a very dirty way and I am looking for an efficient way and I believe there must be some class to do this job. Would you please let me know about that. Thanks again Emran.
-
Hi, /********** Visual Studio 2005, C# .NET 2.0 - Windows Form ***********/ Within my code, I want to hold for certain seconds like Thread.Sleep(2000). But I dont want to block the User Interfaces. Now, I had a choice to use Timer, and When I need to start sleeping, I can call the timer.Start() and use While Loop to check how many seconds passed, if time over then exit the loop, otherwise Application.DoEvents(). But This is a very dirty way and I am looking for an efficient way and I believe there must be some class to do this job. Would you please let me know about that. Thanks again Emran.
Hi! If I understand you correctly, you want a certain action to be performed at a certain time, right? You could do this by creating the timer and setting its
Interval
property to the time difference between now (DateTime.Now
) and the time you want the action to be performed. That way you just have to put your action in the timer'sTick
event handler and you're done. Btw. don't forget to stop the timer in the tick event handler, otherwise you could get the action performed more than once.Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! If I understand you correctly, you want a certain action to be performed at a certain time, right? You could do this by creating the timer and setting its
Interval
property to the time difference between now (DateTime.Now
) and the time you want the action to be performed. That way you just have to put your action in the timer'sTick
event handler and you're done. Btw. don't forget to stop the timer in the tick event handler, otherwise you could get the action performed more than once.Regards, mav -- Black holes are the places where God divided by 0...
Hello, Thanks for your reply. Actually my purpose is not to trigger timed event. Ok, just consider the following code, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; Thread.Sleep(2000); b = c - 1; } Now, in my that code, I want to wait for 2 seconds before executing the last line but If I use Thread.Sleep(2000), then all user interface controls are blocked. An alternative way to perform this task without blocking is to use a thread or backgroudnworker. I can call the Background worker Asynchronously and make a while loop to check if the thread is completed. like this, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; backGroundWorker1.runWorkerAsyncrhonous(); while(backGroundWorker1.IsBusy) Application.DoEvents(); b = c - 1; } backGroundWorker1_DoWork(BWEventargs e.....) { Thread.Sleep(2000); } Now, My question is, IS there any other better short cut way to perform the same sleeping task ? Regards emran
-
bashiwala wrote:
I want to hold for certain seconds like Thread.Sleep(2000). But I dont want to block the User Interfaces.
ummmm.... "hold" what? :rolleyes:
led mike
Hello, Thanks for your reply. Actually my purpose is not to trigger timed event. Ok, just consider the following code, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; Thread.Sleep(2000); b = c - 1; } Now, in my that code, I want to wait for 2 seconds before executing the last line but If I use Thread.Sleep(2000), then all user interface controls are blocked. An alternative way to perform this task without blocking is to use a thread or backgroudnworker. I can call the Background worker Asynchronously and make a while loop to check if the thread is completed. like this, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; backGroundWorker1.runWorkerAsyncrhonous(); while(backGroundWorker1.IsBusy) Application.DoEvents(); b = c - 1; } backGroundWorker1_DoWork(BWEventargs e.....) { Thread.Sleep(2000); } Now, My question is, IS there any other better short cut way to perform the same sleeping task ? Regards emran
-
Hello, Thanks for your reply. Actually my purpose is not to trigger timed event. Ok, just consider the following code, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; Thread.Sleep(2000); b = c - 1; } Now, in my that code, I want to wait for 2 seconds before executing the last line but If I use Thread.Sleep(2000), then all user interface controls are blocked. An alternative way to perform this task without blocking is to use a thread or backgroudnworker. I can call the Background worker Asynchronously and make a while loop to check if the thread is completed. like this, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; backGroundWorker1.runWorkerAsyncrhonous(); while(backGroundWorker1.IsBusy) Application.DoEvents(); b = c - 1; } backGroundWorker1_DoWork(BWEventargs e.....) { Thread.Sleep(2000); } Now, My question is, IS there any other better short cut way to perform the same sleeping task ? Regards emran
You're using the wrong approach. While a method is being executed in your UI thread, the UI will not respond. Using
Application.DoEvents()
is just a workaround for certain cases and shouldn't be used in this case. The right way IMO would be to putb = c - 1;
in a separate method and call this method when a timer elapses.Regards, mav -- Black holes are the places where God divided by 0...
-
Hello, Thanks for your reply. Actually my purpose is not to trigger timed event. Ok, just consider the following code, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; Thread.Sleep(2000); b = c - 1; } Now, in my that code, I want to wait for 2 seconds before executing the last line but If I use Thread.Sleep(2000), then all user interface controls are blocked. An alternative way to perform this task without blocking is to use a thread or backgroudnworker. I can call the Background worker Asynchronously and make a while loop to check if the thread is completed. like this, private void doNothing_Important() { int a = 5; int b = 6; int c = a + b; backGroundWorker1.runWorkerAsyncrhonous(); while(backGroundWorker1.IsBusy) Application.DoEvents(); b = c - 1; } backGroundWorker1_DoWork(BWEventargs e.....) { Thread.Sleep(2000); } Now, My question is, IS there any other better short cut way to perform the same sleeping task ? Regards emran
Maybe somthing like this?
int b;
int c;private void doNothing_Important(){
int a = 5;
b = 6;
c = a + b;// make timer, set to 2 seconds, add handler, start it
System.Windows.Forms.Timer tim = new System.Windows.Forms.Timer();
tim.Tick += new EventHandler(tim_Tick);
tim.Interval = 2000;
tim.Start();
}
private void tim_Tick(object sender, EventArgs e){
// stop timer, remove handler, dispose it
System.Windows.Forms.Timer tim = (System.Windows.Forms.Timer)sender;
tim.Stop()
tim.Tick -= new EventHandler(tim_Tick);
tim.Dispose();// do your thingy
b = c - 1;
}
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
-
Maybe somthing like this?
int b;
int c;private void doNothing_Important(){
int a = 5;
b = 6;
c = a + b;// make timer, set to 2 seconds, add handler, start it
System.Windows.Forms.Timer tim = new System.Windows.Forms.Timer();
tim.Tick += new EventHandler(tim_Tick);
tim.Interval = 2000;
tim.Start();
}
private void tim_Tick(object sender, EventArgs e){
// stop timer, remove handler, dispose it
System.Windows.Forms.Timer tim = (System.Windows.Forms.Timer)sender;
tim.Stop()
tim.Tick -= new EventHandler(tim_Tick);
tim.Dispose();// do your thingy
b = c - 1;
}
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
Thanks Marc, Although, I thought, Application.DoEvents() was equivalent to nonBlocking Sleep function. BEcause, I used following snippet whereever I needed to wait for asynchronous methods, i.e., while (webBrowser1.ReadyState != ReadyState.Complete) Application.doEvents(); Now, according to your solution, I will have to split up my methods in timers. Anyway, if C# doesnot provide any more simpler way... what can I do :), Thanks again,. Emran
-
Thanks Marc, Although, I thought, Application.DoEvents() was equivalent to nonBlocking Sleep function. BEcause, I used following snippet whereever I needed to wait for asynchronous methods, i.e., while (webBrowser1.ReadyState != ReadyState.Complete) Application.doEvents(); Now, according to your solution, I will have to split up my methods in timers. Anyway, if C# doesnot provide any more simpler way... what can I do :), Thanks again,. Emran
Hehe you're welcome :) DoEvents sort of flushes all the windows messages (from the controls and stuff). A method usually isn't supposed to 'hang' for 2 seconds. Like the poster above me said, DoEvents is mostly a workaround, don't use it too often. ;P
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
-
Hehe you're welcome :) DoEvents sort of flushes all the windows messages (from the controls and stuff). A method usually isn't supposed to 'hang' for 2 seconds. Like the poster above me said, DoEvents is mostly a workaround, don't use it too often. ;P
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||
[Marc] wrote:
DoEvents is mostly a workaround
Thanks. I did not get the term "WorkAround". What does it mean ! Can you give me an example ? Thanks and regards
-
[Marc] wrote:
DoEvents is mostly a workaround
Thanks. I did not get the term "WorkAround". What does it mean ! Can you give me an example ? Thanks and regards
Sorry, can't give an example, i never had to use it as fart as i remember. :) A workaround[^] is, as the name states it, that you work around a bug or other weird thing instead of solving the bug/weird thing. Sometimes you can't solve that problem because it's not your fault, and you dont have access to the problems code and so on. Then you have to work around it. :)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
|| Fold With Us! || Pensieve || VG.Net ||