|
-
May 9th, 2012, 10:36 PM
#1
Different option for Thread.Sleep?
Well here is my situation. One of my functions has to wait 3 seconds before switching to the next piece of code;
Code:
foreach (int restart in md.Restart) {
Data.Restart(true);
I have to wait 3 seconds before it executes this line of code:
Code:
Data.Restart(true);
Thread.Sleep is not an option since it freezes my whole program. Could anyone help me out with a method?
Any help would be appreciated thanks!
-
May 9th, 2012, 10:46 PM
#2
Re: Different option for Thread.Sleep?
Spawn a new thread (see the BackgroundWorker class) to carry out the task. Sleeping the background thread will not freeze your whole application.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
May 9th, 2012, 11:02 PM
#3
Re: Different option for Thread.Sleep?
 Originally Posted by BioPhysEngr
Spawn a new thread (see the BackgroundWorker class) to carry out the task. Sleeping the background thread will not freeze your whole application.
Sorry, BackGroundWorker is for forms, correct me if I'm wrong. I'm using Console, is there a way to do it that way?
-
May 10th, 2012, 12:14 AM
#4
Re: Different option for Thread.Sleep?
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
May 10th, 2012, 12:42 AM
#5
Re: Different option for Thread.Sleep?
 Originally Posted by BioPhysEngr
Thank you for the help so far but wouldn't threading a single thread be really uneffecient instead of using a timer function? That's what I need help with, I don't understand how to use timers. for example.
I want the timer to start, stop at 3 seconds, execute the one line of code, then reset. could you explain to me how to do that?
-
May 10th, 2012, 01:02 AM
#6
Re: Different option for Thread.Sleep?
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
May 10th, 2012, 10:17 AM
#7
Re: Different option for Thread.Sleep?
 Originally Posted by BioPhysEngr
I appreciate the help but this still uses Thread.Sleep. I've already googled many times without any result. The timers I was talking about are the ones you import from System.Timers;
-
May 10th, 2012, 12:22 PM
#8
Re: Different option for Thread.Sleep?
You have misunderstood the code.
The first Thread.Sleep() is simply to simulate some amount of work being done while the timer is executing.
The second Thread.Sleep (in ComputeBoundOp) is called on the timer's thread, not the main thread, because Thread.Sleep() always acts on the thread that called it.
Also, no; spawning a single thread would not be "really [in]efficient".
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
May 10th, 2012, 06:57 PM
#9
Re: Different option for Thread.Sleep?
 Originally Posted by BigEd781
You have misunderstood the code.
The first Thread.Sleep() is simply to simulate some amount of work being done while the timer is executing.
The second Thread.Sleep (in ComputeBoundOp) is called on the timer's thread, not the main thread, because Thread.Sleep() always acts on the thread that called it.
Also, no; spawning a single thread would not be "really [in]efficient".
Excuse me while I am very new to C#.
This is what I've done;
Code:
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
//Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(3000); // Simulating other work (10 seconds)
Data.Respawn();
t.Dispose(); // Cancel the timer now
this still freezes all other threads.
What have I done wrong?
-
May 10th, 2012, 07:44 PM
#10
Re: Different option for Thread.Sleep?
 Originally Posted by wutang001
this still freezes all other threads.
No, it doesn't. It suspends the thread it was called on, no others. You need to remove that Sleep. However, if your program simply exits if you remove the sleep.... then I am perplexed as to why you need another thread in the first place. Hanging the main thread in a console app is fine if it is doing work....
If you need to wait three seconds, then that three second sleep should be in the timer's callback method.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
May 10th, 2012, 08:10 PM
#11
Re: Different option for Thread.Sleep?
 Originally Posted by BigEd781
No, it doesn't. It suspends the thread it was called on, no others. You need to remove that Sleep. However, if your program simply exits if you remove the sleep.... then I am perplexed as to why you need another thread in the first place. Hanging the main thread in a console app is fine if it is doing work....
If you need to wait three seconds, then that three second sleep should be in the timer's callback method.
I'm sorry I dont understand what you mean.
What do I have to do here?
-
May 12th, 2012, 01:19 PM
#12
Re: Different option for Thread.Sleep?
 Originally Posted by wutang001
I'm sorry I dont understand what you mean.
What do I have to do here?
Please zip up and post the complete sample solution.
-
May 12th, 2012, 04:10 PM
#13
Re: Different option for Thread.Sleep?
 Originally Posted by Arjay
Please zip up and post the complete sample solution.
This is what I got right now.
Last edited by wutang001; May 12th, 2012 at 07:22 PM.
-
May 12th, 2012, 04:38 PM
#14
Re: Different option for Thread.Sleep?
I didn't ask for a code snippet, I asked for a sample solution.
Look, your code snippet isn't sufficient to show the problem you are having so zip up a sample solution that shows the complete problem.
That way we can load the solution, compile it and may be able to fix what you have into something that works.
Last edited by Arjay; May 12th, 2012 at 06:13 PM.
-
May 12th, 2012, 07:28 PM
#15
Re: Different option for Thread.Sleep?
 Originally Posted by Arjay
I didn't ask for a code snippet, I asked for a sample solution.
Look, your code snippet isn't sufficient to show the problem you are having so zip up a sample solution that shows the complete problem.
That way we can load the solution, compile it and may be able to fix what you have into something that works.
This is the sample
http://www.sendspace.com/file/h38g1i
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|