*
Previous C# Threading QA-11-20 C# Threading QA-31-40 Next

Dot net Framework (C#) Threading Question Answers - 21-30

21. Important Thread Properties

  • IsAlive: True if thread is active.
  • IsBackground: Marks thread as background or foreground.Gets or sets a Boolean that indicates if a thread is or should be a background thread. Background threads are like foreground threads, but a background thread does not prevent a process from stopping. Once all foreground threads that belong to a process have stopped, the common language runtime ends the process by calling the�Abort�method on background threads that are still alive.
  • Name: Gets or sets the name of a thread. Most frequently used to discover individual threads when you debug.
  • Priority: Gets or sets a value that is used by the operating system to prioritize thread scheduling.
  • ThreadState: State of the thread. Contains a value that describes a thread's state or states.
  • Join�: Causes the current thread to wait for another thread to finish. If used with a time-out value, this method returns�True�if the thread finishes in the allocated time.

22. ManualResetEvent inheritable?

No, ManualResetEvent is sealed.

23. STA and MTA

STA (Single Thread Apartment): Used for non-thread-safe COM objects (e.g., UI components).
MTA (Multi Thread Apartment): Used when COM objects handle their own synchronization.

The COM threading model is called an "apartment" model, where the execution context of initialized COM objects is associated with either a single thread (Single Thread Apartment) or many threads (Multi Thread Apartment). In this model, a COM object, once initialized in an apartment, is part of that apartment for the duration of it's runtime.

The STA model is used for COM objects that are not thread safe. That means they do not handle their own synchronization. A common use of this is a UI component. So if another thread needs to interact with the object (such as pushing a button in a form) then the message is marshalled onto the STA thread. The windows forms message pumping system is an example of this.

If the COM object can handle its own synchronization then the MTA model can be used where multiple threads are allowed to interact with the object without marshalled calls.

24. Event Wait Handles in C#

Event wait handles synchronize threads across boundaries. Examples:

  • EventWaitHandle
  • AutoResetEvent
  • ManualResetEvent
  • CountdownEvent
  • ManualResetEventSlim (.NET 4.0+)

The Event Wait handles are EventWaitHandle, AutoResetEvent, ManualResetEvent, and CountdownEvent. And in .net 4.0 ManualResetEventSlim is a lightweight event for synchronization within a single process boundary.

25. Synchronization constructs

  • Simple blocking methods: These wait for another thread to finish or for a period of time to elapse. Sleep, Join, Task.Wait
  • Locking constructs: lock, Monitor, Mutex, SpinLock, Semaphore . These limit the number of threads that can perform some activity or execute a section of code at a time. Exclusive locking constructs are most common�these allow just one thread in at a time, and allow competing threads to access common data without interfering with each other. The standard exclusive locking constructs are lock (Monitor.Enter/Monitor.Exit), Mutex, and SpinLock. The nonexclusive locking constructs are Semaphore, SemaphoreSlim, and the reader/writer locks.
  • Signaling constructs: These allow a thread to pause until receiving a notification from another, avoiding the need for inefficient polling. There are two commonly used signaling devices: event wait handles and Monitor�s Wait/Pulse methods. Framework 4.0 introduces the CountdownEvent and Barrier classes.
  • Nonblocking synchronization constructs: These protect access to a common field by calling upon processor primitives. The CLR and C# provide the following nonblocking constructs: Thread.MemoryBarrier, Thread.VolatileRead, Thread.VolatileWrite, the volatile keyword, and the Interlocked class.

26. Thread priorities

There are 5 Priorities in Thread: Lowest, BelowNormal, Normal, AboveNormal, Highest

27. Ways to create a thread

  • Thread object:Create Thread Object and call the start method
Thread t = new Thread(WriteY); // Kick off a new thread WriteY is the method Name.
t.Start();
  • ThreadPool: By Joining the ThreadPool, again there are many ways to join the Thread Pool.
ThreadPool.QueueUserWorkItem(DoWork);
ThreadPool.QueueUserWorkItem(DoWork, 56);

static void DoWork(object data)
{
  Console.WriteLine("Hi from the thread pool! " + data);
}

// Output:
Hi from the thread pool!
Hi from the thread pool! 56

28. Ways to join the thread pool

  • ThreadPool.QueueUserWorkItem
  • Task Parallel Library (TPL)
  • Asynchronous delegates
  • BackgroundWorker

29. What is "preempted" in threading?

A thread is preempted when interrupted by external factors (e.g., time-slicing). It cannot control when this happens.

30. What is AutoResetEvent?

AutoResetEvent signals one waiting thread, then automatically resets to non-signaled state. If no threads are waiting, it stays signaled until one waits.

AutoResetEvent notifies a waiting thread that an event has occurred.

A thread waits for a signal by calling WaitOne on the AutoResetEvent. If the AutoResetEvent is in the non-signaled state, the thread blocks, waiting for the thread that currently controls the resource to signal that the resource is available by calling Set.

Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely.

If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block. The AutoResetEvent releases the thread immediately and returns to the non-signaled state.

Back to Index
Previous C# Threading QA-11-20 C# Threading QA-31-40 Next
*
*