View Single Post
Old 09-02-2011, 06:05 PM  
WarChild
Let slip the dogs of war.
 
WarChild's Avatar
 
Industry Role:
Join Date: Jan 2003
Location: Bermuda
Posts: 17,263
Quote:
Originally Posted by tical View Post
If you've got the Thread.Sleep() in a Form it will hang your app. I use it to pause / slow down threads all of the time. You just have to use it the right way to avoid making it seem like it's hanging your app.

If you're calling something like:

void dowork
{
// some code
// loop w/ sleep
// other code
}

Then "other code" won't execute until the loop w/ sleep is done, so if there are a ton of listids then it might seem like it's lagging. Plus your form is going to get "stuck" during each sleep iteration.

If you want to have it run in the background without having that error. Then do something like this:

void listidloop
{
// your loop code w/ thread.sleep, etc.
}

and in the function you're calling it from (form or other class):

Thread t = new Thread(listidloop);
t.Start();

That way it operates in a separate thread and doesn't lock up your form. The problem here is if the listidloop function is trying to update the form form (ie, textbox1.text = "", etc) then you're going to get a cross thread error and will need to use delegates/invoking for this.
If you're going to do it in a sepeare thread, it might be worth throwing in an application.processmessages too.
__________________
.
WarChild is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote