Reputation: 1357
I want to create a small delay so that my 1st set of codes run smoothly.
How can I do that in vb.net ?
Edit 1
Suppose I have a few lines of code like this
..................Statement Line 1..............
..................Statement Line 2..............
..................Statement Line 3..............
..................Statement Line 1..............
..................Statement Line 5..............
WAIT UNTIL STATEMENT 5 IS COMPLETED
..................Statement Line 6..............
..................Statement Line 7..............
..................Statement Line 8..............
..................Statement Line 9..............
..................Statement Line 10.............
Only when the execution of first five statements are complete then only the next five can be executed
Upvotes: 2
Views: 9200
Reputation: 2866
Use BackgroundWorker object property "IsBusy" and do not allow to execute line 6 until the worker process is busy.
Read more about BackgroundWorker here
Upvotes: 0
Reputation: 15071
First - given the code sample you've provided line 6 will not execute until line 5 finishes. You don't need to do anything; unless line 5 is kicking of an external application or creating a new thread.
Beyond that -
Thread.Sleep
will introduce a delay, but more often than not it really isn't what you are looking for.
If you use Thread.Sleep the executing thread will sleep for however long you tell it. But your sample code indicates you want the thread to wait UNTIL some condition is met. Assuming you are waiting on a condition that happens outside of the thread you are sleeping, at best, you'd end up with a loop that keeps sleeping for X milliseconds and then checking the condition.
There are other approaches that are easier (in the long run)/more robust than that. If you truly want something to happen on another thread and to be alerted by it's completion; consider the BackgroundWorker class.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
It's very handy for simple multithreading tasks. You create a BackgroundWorker and handle it's 'DoWork' event with the logic to happen on the new thread and handle the 'Completed' event (check the docs for the correct name, I'm going from memory). You call 'RunWorkerAsynch' to start the process and when the 'Worker_Completed' event fires, you can continue execution of line 6.
I hope that makes sense/helps.
Upvotes: 3
Reputation: 4480
Are you looking to make a thread sleep?
Thread.Sleep(100);
Where 100 is the number of millisecond you want the thread to sleep for.
Also make sure to have Imports System.Threading, which I assume you have if you already have multiple threads.
EDIT: Okay, so you've added a bit of code. Still, this should come down to whether you have more than one thread running, and from your question it looks like it's all in one thread. In this case, statement 5 will always finish first before statement 6 runs. That's how code works. The only case where it wouldn't would be if one of statements 1-5 spawns something on a new thread.
Upvotes: 3