Understanding threads and threading
In this section, we will understand the life cycle of threads. Threads in C# have a life cycle as follows:
When started, threads enter the running state. When running a thread, there is a possibility it will enter a wait, sleep, join, stop, or suspended state. A thread is suspended by calling the Suspend
method, and calling the Resume
method resumes a thread.
When the Monitor.Wait(object obj)
method is called, the thread enters the wait
state. A waiting thread will continue when the Monitor.Pulse(object obj)
method is called, and you can make threads sleep by calling the Thread.Sleep(int millisecondsTimeout)
method.
When you call the Thread.Join()
method, it causes the thread to enter the wait
state. The waiting thread will then continue once the dependent threads have completed running. If any dependent threads are canceled, the thread is aborted and enters the stop
state...