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!
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.
Re: Different option for Thread.Sleep?
Quote:
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?
Re: Different option for Thread.Sleep?
Re: Different option for Thread.Sleep?
Quote:
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?
Re: Different option for Thread.Sleep?
Re: Different option for Thread.Sleep?
Quote:
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;
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".
Re: Different option for Thread.Sleep?
Quote:
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?
Re: Different option for Thread.Sleep?
Quote:
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.
Re: Different option for Thread.Sleep?
Quote:
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?
Re: Different option for Thread.Sleep?
Quote:
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.
Re: Different option for Thread.Sleep?
Quote:
Originally Posted by
Arjay
Please zip up and post the complete sample solution.
This is what I got right now.
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.
Re: Different option for Thread.Sleep?
Quote:
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
Re: Different option for Thread.Sleep?
I don't have access and I'm not willing to sign up.
Can you SIMPLY attach the zipped file in your reply (use the Manage Attachments button)?
1 Attachment(s)
Re: Different option for Thread.Sleep?
Quote:
Originally Posted by
Arjay
I don't have access and I'm not willing to sign up.
Can you SIMPLY attach the zipped file in your reply (use the Manage Attachments button)?
Attachment 29622
Here you go, I really appreciate the help.
Re: Different option for Thread.Sleep?
Modify the Code class...
Code:
class Data
{
public void Restart()
{
ThreadPool.QueueUserWorkItem(DoWork);
}
private void DoWork(object dataState)
{
Thread.Sleep(3000);
Console.WriteLine("Started!");
}
}
This will delay the Restart operation without blocking the main thread. Of course it probably doesn't do what you really need it to do.
If not, then repost a more complete zipped solution where the sample code shows code that is a bit closer to what you really need.
Re: Different option for Thread.Sleep?
Quote:
Originally Posted by
Arjay
Modify the Code class...
Code:
class Data
{
public void Restart()
{
ThreadPool.QueueUserWorkItem(DoWork);
}
private void DoWork(object dataState)
{
Thread.Sleep(3000);
Console.WriteLine("Started!");
}
}
This will delay the Restart operation without blocking the main thread. Of course it probably doesn't do what you really need it to do.
If not, then repost a more complete zipped solution where the sample code shows code that is a bit closer to what you really need.
Thanks! that worked just fine!