Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi all,
I need to stop the execution of the code for some while. I used thread.sleep() but in the loop it's not behaving correctly and instantly executing next lines of code.
For example:

C#
// If this is the first line 
string abc = "This is first line";

// and after that I want to pause the execution of the application for 5 seconds and 
// then want to execute below line.

MessageBox.Show(abc);


Please tell me any alternative for the thread.sleep().
Posted
Updated 25-Mar-19 2:02am
v2
Comments
PIEBALDconsult 21-Nov-14 10:18am    
Where's the code? Do you realize that Sleep uses milliseconds?
DamithSL 21-Nov-14 10:27am    
update the question with your actual code
Philippe Mori 21-Nov-14 19:53pm    
Why would not want to wait 5 seconds before displaying a message box? This is not user friendly as the user won't why nothing happen during that time.

You'll have to rethink your design (both the UI and the code) to make something more usable.
Member 7695634 24-Nov-14 7:10am    
Thanks Philippe for your suggestion. By the way, I am not trying to display message after 5 sec. I gave an example just to know that if there any way to pause the execution of the code for a while.

You can - it's not difficult:
C#
Sleep(5000);
Will do it.

But...it'll lock up your user interface until the time has passed.
If you want to do anything else at the time (or you don't like your user thinking your app has crashed) then I'd consider using a Timer, or even a BackgroundWorker for the long task
 
Share this answer
 
Comments
BillWoodruff 21-Nov-14 12:30pm    
+5 You are the only one responding here that raised the important issue of application responsiveness (fyi: I didn't downvote the other replies).
frostcox 21-Nov-14 19:03pm    
async springs to mind!!!
If you look at the intellisense or on MSDN, Thread.Sleep() takes an argument that is the value of time to "sleep" the thread in milliseconds.

To have your code wait 5 seconds before showing the messagebox, try:

C#
Thread.Sleep(5000);
MessageBox.Show(abc);



You will need to include:

using System.Threading;


in your namespaces, otherwise use the fully qualified path to the method:

System.Threading.Thread.Sleep(5000);


Well, if for some bizarre reason your thread.sleep() isn't working (I'd REALLY want to figure this one out because it means there's something else happening - likely you have another thread re-entering some method) you can always try this:

C#
var waitTime = new TimeSpan(0, 0, 5);
var waitUntil = DateTime.Now + waitTime;

while(DateTime.Now <= waitUntil)
{
    System.Threading.Thread.Sleep(1000);
    // .
    // .
    // .
}

MessageBox.Show(abc);


If that still doesn't work, you have OTHER problems either in code or within your system.
 
Share this answer
 
v2
Comments
Member 7695634 24-Nov-14 7:48am    
Thanks a lot guys for your replies and I am very sorry for the (typing) mistake that I had done in the above question. I know that Thread.Sleep() method takes an argument and I had used Thread.Sleep(5000). But I really don't know why it is not working. As I said the code is in the for loop. Initially thread.sleep(5000) responds accurately but after 10 records it executes the code very fast (even less than a second). Why this is happening I don't know.
Thread.Sleep[^] should work just fine.

Look at the code below to start with:

C#
string message = "Hello world!";

for (int count = 0; count < 10; count++)
{
    Console.WriteLine(string.Format("{0}: {1}", count, message));

    //wait for 2 seconds
    Thread.Sleep(2000);
}
 
Share this answer
 
You should pass a parameter in integer values to be used by the application for stopping. Try this one,

C#
// thread1 is your thread, for instance
thread1.Sleep(5000); // 5000ms = 5seconds


This would pause the thread for 5 seconds.
 
Share this answer
 
v2
private async void Function()
{
string message = "Hello world!";
await Task.Delay(3000);
MessageBox.Show(message );
}

try this
 
Share this answer
 
Comments
CHill60 25-Mar-19 8:38am    
You have added nothing to the thread that hasn't already been mentioned
Richard Deeming 26-Mar-19 16:11pm    

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