Saturday, May 29, 2010

Wait for a While

In this post I want to show how to make the C# application wait for some time and resume its run only after the elapse of this time This purpose serves the function System.Threading.Thread.Sleep().
It stops the current thread to wait for the time specified as parameter (in milliseconds).
A thread is kind of a subprocess of a process. A C# application is started in its own process, but since there are of course other running processes on the operating system, the operating system switches between these continously, the current active one is always executed.
Now in our C# application we can create multiple threads.
These seem to run independent of each other, if the application process gets the focus it switches itself between the different threads, so we get an impression of parallel execution. A "normal" Windows Forms - Application consists of only one thread though, so that the following code halts the whole program for 1 second:
System.Threading.Thread.Sleep(1000);

No comments:

Post a Comment