Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
84 Cards in this Set
- Front
- Back
What is the Thread class for
|
1. Creates and controls a thread.
2. Sets its priority. 3. Gets its status." |
|
What is the ThreadPool class for
|
To provide a recyclable pool of worker threads, managed by the system, that can be used to:
1. Execute tasks. 2. Post work items. 3. Process asynchronous I/O. 4. Wait on behalf of other threads. 5. Process timers. |
|
What is a ThreadStart delegate for
|
Represents a parameterless method that is executed by a thread that is explicitly started (not a pool-thread). Note that the explicit instantiation of this delegate is not needed in the latest versions of C# (you can just put the name of the method, rather than "new ThreadStart(...)"
|
|
What is a ParameterizedThreadStart delegate
|
Represents the method that executes on a Thread that is explicitly started (not a pool-thread). It allows Thread.Start to accept an Object:
new Thread(Print).Start("Hello!"); void Print(object msgObj) { var msg = (string) msgObj; // cast needed (msg == "Hello!") Console.WriteLine(msg); } //Note: this is simpler with a lambda expression: new Thread(() => Console.Write("Hello!")).Start(); |
|
What does the SynchronizationContext class do.
|
Provides the basic functionality for propagating a synchronization context in various models (marshall code between threads). Here's how to "post" to a UI control from a worker thread:
var _uiSc = SynchronizationContext.Current; //static prop new Thread(Work).Start(); void Work() { _uiSc.Post( _ => txtMessage.Text = message); } (useful, because this works for WPF, Metro & Windows Forms) |
|
What is the Timeout class
|
Static class that contains constants used to specify an infinite amount of time, ie:
Thread.Sleep(Timeout.Infinite); Thread.Sleep(Timeout.InfiniteTimeSpan); // for overload that expects a TimeSpan |
|
What is the Timer class, and how is it used.
|
Uses a pooled thread to generates recurring events in an application:
var tmr = new Timer(Tick, "tick ...", 5000, 1000) Console.ReadLine(); // to allow timer to work tmr.Dispose(); // stops timer & cleans up void Tick(object data) { Console.WriteLine(data); // writes "tick ... tick ..., etc" } |
|
Explain the AutoResetEvent class, with a code snippet.
|
A sealed class that lets ONE waiting thread "through" when "set" by another. Works "like" a ticket-turnstile: when Set is called by another thread (a "ticket" is supplied) the turnstile lets ONE thread through, and immediately closes again. Snippet:
var _aRE = new AutoResetEvent(false); new Thread(Waiter).Start(); Thread.Sleep(1000); // pause for a sec _aRE.Set(); // let one waiter through void Waiter() { Console.WriteLine("Waiting ..."); _aRE.WaitOne(); // wait til this one lets you through Console.WriteLine("Made it!); } //output Waiting... <pause> Made it! |
|
What is a TimerCallback delegate
|
It represents the method that handles calls from a Timer:
public delegate void TimerCallback(object state) |
|
What is the WaitCallback delegate (show code snippet of its use)
|
Represents a callback method to be executed by a thread pool thread (not by one we start in code). The method is to be executed immediately if a thread is available:
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWork), "Hello"); void ThreadWork(object msg) { Console.WriteLine((string)msg); } |
|
What is the WaitHandle class
|
Abstract class that encapsulates operating system–specific objects to allow threads to wait for exclusive access to shared resources. Implemented by these classes:
1. EventWaitHandle i) AutoResetEvent ii) ManualResetEvent 2. Mutex 3. Semaphore |
|
What is a WaitOrTimerCallback delegate
|
It represents a method to be called (on a "pool" thread) when a WaitHandle is signaled or times out.
|
|
What is a ThreadExceptionEventArgs class
|
Provides data for the ThreadException event.
|
|
What is a ThreadExceptionEventHandler Delegate
|
Represents the method that will handle the ThreadException event of an Application.
|
|
What is the ThreadState enumeration class for, and how does it maintain data.
|
Specifies the execution states of a Thread.
This enumeration has a ComVisibleAttribute attribute that allows a bitwise combination of its member values. |
|
What is the purpose of the ThreadPriority enumeration class.
What enumeration values does it contain? |
To specify the scheduling priority of a Thread.
enum ThreadPriority {Lowest, BelowNormal, Normal, AboveNormal, Highest} |
|
ReaderWriterLock class
|
Defines a lock that supports single writers and multiple readers.
|
|
Explain the ManualResetEvent class
|
It's a sealed class that works as a simple gate: calling Set opens the gate allowing through ANY number of threads that are calling WaitOne.
|
|
IAsyncResult interface
|
Represents the status of an asynchronous operation.
|
|
EventWaitHandle class
|
Represents a thread synchronization event.
|
|
What is the RegisteredWaitHandle class & what is used for. Write code snippet.
|
This is a sealed class that represents a reference to a registration action (with the thread pool) that results by calling RegisterWaitForSingleObject to schedule a delegate execution upon an wait-handle signaling, or timeout.
AutoResetEvent ev = new AutoResetEvent(false); RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(ev, WaitProc, "data", 1000, false); ... ev.Set(); public void WaitProc(object data, bool timeOut) { // ... do something } Useful so a thread does not have to wait on a wait handle (i.e.: AutoResetEvent "ev" above) to be signaled. Instead, this attaches a "continuation" method (WaitProc) to be automatically executed when the handle is signaled/timeout. |
|
What is the SendOrPostCallback delegate
|
It represents a method to be called when a message is to be dispatched (send/post) to another thread (usually the UI thread) using a synchronization context?
Here's how a working thread can reach back to the UI thread: var uiSynchContext = SynchronizationContext.Current; // in UI thread // .... later (in worker thread) note: UpdateUI is the delegate here uiSychContext.Post(UpdateUI, "new msg"); void UpdateUI(object msg) { uiTextBox.Text = (string) msg; } |
|
What is the function of the IOCompletionCallback delegate.
|
To receive the error code, number of bytes, and overlapped value type when an I/O operation completes on the thread pool.
|
|
Interlocked class
|
Provides atomic operations for variables that are shared by multiple threads.
|
|
What is the NativeOverlapped structure
|
Provides an explicit layout that is visible from unmanaged code and that will have the same layout as the Win32 OVERLAPPED structure with additional reserved fields at the end.
|
|
What is the Overlapped class
|
Provides a managed representation of a Win32 OVERLAPPED structure, including methods to transfer information from an Overlapped instance to a NativeOverlapped structure.
|
|
What is the ExecutionContext class
|
A sealed class that manages the execution context for the current thread.
|
|
What is the HostExecutionContext class
|
Encapsulates and propagates the host execution context across threads.
|
|
What does the HostExecutionContextManager class provide.
|
Provides the functionality that allows a common language runtime host to participate in the flow, or migration, of the execution context.
|
|
Explain the ContextCallback delegate
|
Used by ExecutionContext.Run & SecurityContext.Run methods. It represents a method to be called within the new ExecutionContext. After the execution ends, the previous context is restored.
Signature: public delegate void ContextCallback(Object state) |
|
What is the LockCookie structure
|
A value type that defines the lock that implements single-writer/multiple-reader semantics. It can be used to "upgrade" the lock from "reader lock" to "writer lock" and viceversa.
|
|
What does the Monitor class provide.
|
Provides a mechanism that synchronizes access to objects. Methods include Enter, Wait, Pulse, PulseAll, Exit.
|
|
What is the Mutex class
|
A synchronization primitive that can also be used for interprocess synchronization.
|
|
Explain a difference between the Semaphore and the Monitor classes.
|
The Semaphore class, unlike Monitor, can be used with WaitHandle.WaitAll and WaitAny, and can be passed across AppDomain boundaries.
|
|
What is the ISynchronizeInvoke interface
|
Provides a way to synchronously or asynchronously execute a delegate. Has these methods:
1. BeginInvoke (asynchronous begin) 2. EndInvoke (asynchronous end) 3. Invoke (synchronous/blocking) & this property: InvokeRequired (true if we're in another thread ... and thus require above methods, rather than directly updating, for instance, the UI element. |
|
What class is used to:
1. Create and controls a thread. 2. Sets its priority. 3. Gets its status. |
The Thread class
|
|
What represents a parameterless method that executes on an explicitly started Thread (not from the pool)
|
ThreadStart delegate
|
|
What represents the method that executes on a (not thread-pool) Thread that allows Thread.Start to accept an Object.
|
ParameterizedThreadStart delegate
|
|
What class provides the basic functionality for propagating a synchronization context in various synchronization models.
|
SynchronizationContext class
|
|
What sealed class contains a constant used to specify an infinite amount of time.
|
Timeout class
|
|
What class is capable of generating recurring events in an application.
|
The Timer class
|
|
What class notifies a waiting thread that an event has occurred.
|
AutoResetEvent class
|
|
What represents the method that handles calls from a Timer.
|
The TimerCallback delegate
|
|
What represents the (thread-work) method called by the QueueUserWorkItem method of the ThreadPool class.
|
WaitCallback delegate
|
|
What class encapsulates operating system–specific objects that wait for exclusive access to shared resources.
|
WaitHandle class
|
|
What represents a method to be called in a "pool" thread when a WaitHandle is signaled or times out.
|
WaitOrTimerCallback delegate
|
|
What class provides data for the ThreadException event.
|
The ThreadExceptionEventArgs class
|
|
What represents the method that will handle the ThreadException event of an Application.
|
ThreadExceptionEventHandler Delegate
|
|
What enumeration class:
1. Specifies the execution states of a Thread. 2. Has a ComVisibleAttribute attribute that allows a bitwise combination of its member values. |
ThreadState enumeration
|
|
What class is used to specify the scheduling priority of a Thread.
|
ThreadPriority enumeration
|
|
What class defines a lock that supports single writers and multiple readers.
|
The ReaderWriterLock class
|
|
What class notifies one, or more, waiting threads that an event has occurred & allows multiple threads to proceed.
|
The ManualResetEvent class. It works as a simple "gate": ... when signaled, all threads that are blocking waiting for this event are "let through". It stays in this (unblocking) state until it is manually reset.
|
|
What class represents the status of an asynchronous operation.
|
The IAsyncResult interface
|
|
What class represents a thread synchronization event.
|
The EventWaitHandle class
|
|
What class represents a handle that has been registered by calling RegisterWaitForSingleObject.
|
RegisteredWaitHandle class
|
|
What represents a method to be called when a message is to be dispatched (send/post) to another thread (usually the UI thread) using a synchronization context?
|
The SendOrPostCallback delegate.
Here's how a working thread can reach back to the UI thread: // in UI thread we "store" the UI context var uiSynchContext = SynchronizationContext.Current; // .... later (in worker thread) run delegate UpdateUI uiSychContext.Post(UpdateUI, "new msg"); // this method will run in the UI context void UpdateUI(object msg) { uiTextBox.Text = (string) msg; } |
|
What represents a method to be called, when an I/O operation completes on a thread pool, and which receives the error code, number of bytes, and overlapped value type.
|
IOCompletionCallback delegate
|
|
What class provides atomic operations for variables that are shared by multiple threads.
|
The "Interlocked" class. Example, for thread safety, use:
int count = 0; Interlocked.Increment(ref count); // static method ... instead of just: count++; Increment: loads/increments/stores in one atomic operation. (other ops: Decrement, Exchange, Read, etc.) |
|
What structure provides an explicit layout that is visible from unmanaged code and that will have the same layout as the Win32 OVERLAPPED structure with additional reserved fields at the end.
|
NativeOverlapped structure
|
|
What Provides a managed representation of a Win32 OVERLAPPED structure, including methods to transfer information from an Overlapped instance to a NativeOverlapped structure.
|
Overlapped class
|
|
What sealed class manages the execution context for the current thread.
|
ExecutionContext class
|
|
What class encapsulates and propagates the host execution context across threads.
|
HostExecutionContext class
|
|
What class provides the functionality that allows a common language runtime host to participate in the flow, or migration, of the execution context.
|
HostExecutionContextManager class
|
|
What represents/stands-for the method to be called within a new context.
|
ContextCallback delegate
|
|
What value type defines the lock that implements single-writer/multiple-reader semantics.
|
LockCookie structure. Use:
static int resource = 0; var rwl = new ReaderWriterLock(); rwl.AcquireReaderLock(1000); // 1 sec timeout ... Console.WriteLine("resource = {0}" + resource); // safe to read ... // If needed upgrade to "writer" lock LockCookie lc = rwl.UpgradeToWriterLock(1000); resource = 123; // safe to change value ... rwl.DowngradeFromWriterLock(ref lc); Console.WriteLine("resource still = {0}", resource); // still safe to read ... rwl.ReleaseReaderLock(); // ensure lock is released |
|
What class provides a mechanism that synchronizes access to objects, including: Enter, Wait, Pulse, PulseAll & Exit.
|
Monitor class
|
|
What class is another synchronization primitive that can also be used for interprocess synchronization.
|
Mutex class
|
|
What synchronization class, unlike Monitor, can be used with WaitHandle.WaitAll and WaitAny, and can be passed across AppDomain boundaries.
|
Semaphore class
|
|
What interface provides a way to synchronously or asynchronously execute a delegate. Show code snippet of invocation.
|
The ISynchronizeInvoke interface provides these methods:
1. BeginInvoke (asynch begin) 2. EndInvoke ( asynch end ) 3. Invoke (synchronous) 4. InvokeRequired (true if worker thread is different than one implementing this interface) Example: snippet above (from codeproject.com) shows an event handler that updates a Windows form field. If worker thread is different (than UI thread) it will use Begin invoke, otherwise (if we're in the same UI thread) then the field is directly updated. |
|
What methods are used to delineate a section of code that cannot be safely aborted?
|
Thread.BeginCriticalRegion();
... // do critical work here. Thread.EndCriticalRegion(); // Both static |
|
What are the ThreadState Enumeration items?
|
1. Running
2. Stopped 3. Stop Requested 4. Aborted 5. Abort Requested, 6. Background |
|
What Thread method is used to block the calling thread until the thread terminates.
|
Join
|
|
What are some Interlocked methods
|
Add, Decrement, Exchange, Increment, Read
|
|
What class does lock use?
|
It uses the Monitor class. The "lock" statement is a shortcut for calling the "Enter" & "Exit" methods of the Monitor class.
|
|
What are the monitor methods
|
1. Wait
2. TryEnter, 3. Enter 4. Exit |
|
What class do Mutex, Semaphore, AutoRestEvent, and ManualRestEvetn all inherit from?
|
WaitHandle class
|
|
What is mutex short for?
|
Mutual exclusion
|
|
How long does the AutoResetEvent stay signaled once set.
|
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. |
|
How long does the ManualResetEvent stay signaled once set.
|
Once it has been signaled, ManualResetEvent remains signaled until it is manually reset
|
|
What are the three styles of programming with the APM?
|
Wait-Until-Done, Polling, CallBack
|
|
What class Provides a recyclable pool of worker threads, managed by the system, that can be used to:
1. Execute Tasks. 2. Post work items. 3. process asynchronous I/O. 4. Wait on behalf of other threads. 5. Process timers. |
The ThreadPool class
|
|
Write a line of code to create a Task that executes a simple "Console.WriteLine(" ... ")"
|
Task.Run(() => Console.WriteLine(" ... "));
|
|
What does the [STAThread] attribute do?
|
It indicates that the COM threading model for the application is "single-threaded apartment". This attribute must be present on the entry point of any application that uses Windows Forms because, although core Windows Forms does not use COM, many components of the OS such as system dialogs do use this technology.
|
|
Does locking (using the lock statement) restrict access to the synchronization object?
For instance: if a thread calls lock(x) ... can another thread execute x++? |
Locking does NOT restrict access to the synchronization object itself. However, a restriction is that ONLY ONE thread can lock the synchronization object at a time. Any other attempting this would be queued.
|