Threading
Thread th = new Thread(new ThreadStart(Test));
ThreadStart is a delegate which doesn't take parameter, we can give a reference to the method which do not take any parameter.
Thread.Sleep(3000); //will cause the current thread to sleep for 3 secs, i.e. 3000 ms.
th.Join(); //if any other thread has no finished its task it will block the main thread till the another thread finishes
ThreadPriority Enumeration consists of values that can be use to set or get the thread priority.
ThreadPriority.Highest, ThreadPriority.Lowest, ThreadPriority.Normal etc.
To pass information to the thread use delegate ParameterizedThreadStart
E.g.
//function to be called by the ParameterizedThreadStart
public static void TestParametereized(int i)
{
}
ParameterizedThreadStart pt = new ParameterizedThreadStart(TestParametereized);
Thread thP = new Thread(pt);
thP.Start(10);
// to stop the thread, system prepares ThreadAbortException, whether caught
// or not thread will be stopped.
thP.Abort();
Code between Thread.BeginCriticalRegion(); and Thread.EndCriticalRegion(); is treated as a single line of code, while executing the code between this block if the thread receives a Abort Signal it will complete executing the code between this block and then die throwing ThreadAbortException, this way it will leave the object in a consistent state.
Q) What type of object is required when starting a thread that requires a single parameter?
A) ThreadStart delegate.
Q) What method stops a running thread?
A) Thread.Abort
Sharing Data Multi Threading
Synchronization with windows kernel objects:
Mutex : Allows synchronization (locking mechanism) across AppDomain and Process boundries, e.g. Single instance application.
Semaphore
Event : Provides a way to notify to multiple threads that event has occured.
Mutex, Semaphore, AutoResetEvent, ManualResetEvent all derive from WaitHandle class.
On mutex created with a well-known name, one can use OpenExisting method to get a Mutex that has already been created.
Event class are type of kernel objects that has two states, on and off. These states allow threads across an application to wait until an event is signaled to do something specific.
Asynchronous Programming Model
Three styles to deal with handling of the end of the call in an asynchronous call.
Wait until done
Polling
Callback
Wait until done: Use BeginInvoke methode and pass null for callback and stateobject. E.g. BeginRead of FileStream, call EndInvoke (e.g. EndRead) this will block the thread until the asynchronous call is done.
E.g.
IAsyncResult iResult = fw.BeginRead(buffer,0,buffer.Length,null,null);
int nByteRead = fw.EndRead(iResult); //thread will be blocked, until the asynch is done.
Polling: Use BeginRead and get the reference to AsynchResult, Code will poll IAsyncResult to see whether it is completed.
E.g.
IAsyncResult iResult = fw.BeginRead(buffer,0,buffer.Length,null,null,);
while(!iResult.IsCompleted)
{
Thread.Sleep(1000);
}
Callback Model:
System.Globalization to address the geogrophical scope of the application
Invariant Culture: This culture category is culture-insenistive, useful when creating trial application, not based on English language but is associated with it and bears more similiarity with it.
Neutral Culture: English, French, and Spanish are all neutral culture, a neutral culture is associated with Language but has no relationship to countries or region. A neutral culture is designated by the first two characters in CultureInfo class.
Specific Culture: Most Precise of three, represented by a neutral culture, a hyphen and then a specific culture abbrevation. e.g. fr-FR, en-US.
E.g. to set a culture of current thread.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Console.WriteLine((1000).ToString("C"));
Wednesday, October 31, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment