Tuesday 27 December 2011

DotNet Frequently Asked Questions on Threading

What does AddressOf operator do in background ?


The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread.

How can you reference current thread of the method ?


"Thread.CurrentThread" refers to the current thread running in the method."CurrentThread" is a public static property.

What's Thread.Sleep() in threading ?


Thread's execution can be paused by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. Example Thread.CurrentThread.Sleep(2000).

How can we make a thread sleep for infinite period ?


You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method.

What is Suspend and Resume in Threading ?


It is Similar to Sleep and Interrupt. Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state.

Note :- In threading interviews most people get confused with Sleep and Suspend. They look very similar.



What the way to stop a long running thread ?


Thread.Abort() stops the thread execution at that moment itself.

How do I debug thread ?


Debug thread window


This window is only seen when the program is running in debug mode. In windows one of the window is “Threads”.



What is Thread.Join() in threading ?


There are two versions of Thread.Join :-

  • Thread.join().

  • Thread.join(Integer) this returns a Boolean value.


The Thread.Join method is useful for determining if a thread has completed before starting another task. The Join method waits a specified amount of time for a thread to end. If the thread ends before the time-out, Join returns true; otherwise it returns False. Once you call Join, the calling procedure stops and waits for the thread to signal that it is done.

Example you have "Thread1" and "Thread2" and while executing 'Thread1" you call "Thread2.Join()".So "Thread1" will wait until "Thread2" has completed its execution and the again invoke "Thread1".

Thread.Join(Integer) ensures that threads do not wait for a long time. If it exceeds a specific time which is provided in integer the waiting thread will start.

No comments:

Post a Comment