| C# Threading QA-11-20 | C# Threading QA-31-40 | |
Dot net Framework (C#) Threading Question Answers - 21-30 |
No, ManualResetEvent is sealed.
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.
Event wait handles synchronize threads across boundaries. Examples:
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.
There are 5 Priorities in Thread: Lowest, BelowNormal, Normal, AboveNormal, Highest
Thread t = new Thread(WriteY); // Kick off a new thread WriteY is the method Name. t.Start();
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
A thread is preempted when interrupted by external factors (e.g., time-slicing). It cannot control when this happens.
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.
| C# Threading QA-11-20 | C# Threading QA-31-40 | |