Threading

Always test on a multiprocessor machine
The greatest challenge is that of synchronization
Do not make any assumptions about OS threads and managed threads
You can think of threads as independent execution paths, which can access resources such as memory.
The majority of the classes you need can be found in the following namespace.

using System.Threading 

System.Threading

This namespace contains everything relating to threading
A process can create one or more threads associated with a process
Use a ThreadStart delegate to specify the program code executed by the thread.
For the duration of a thread its state is always one of the ThreadState enumerations
GetHashCode provides identification for managed threads.


public class MyThread 
{
   public static void ThreadProc()
    {
         for (int i=0;i<10; i++)
         {
             System.Console.Writeline("My Thread: {0}, i);
             System.Threading.Thread.Sleep(0);
         }
     }

   public static void Main()
   {
       System.Threading.Thread mythread;
       mythread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
       mythread.IsBackground = true;
       mythread.Start();

       myThread.Join();
   }
}

Interlock class ?



Pre-emptive Multitasking - A thread can be suspended at almost any time and another thread can be given CPU time


Each thread maintains a private set of instructions that the operating system uses to save information (the thread context) when the thread is not running, including the values of CPU registers at the time when the thread was suspended and the processor was allocated to another thread.
A thread also maintains its exception handlers and a prority level.
You can assign higher priority to threads that manage the user interface (so it is more responsive) and lower priority to threads for less urgent tasks such as background printing.
In all cases the time slice allocated to each thread is relatively short so the end user has the perception that all the threads (and all applications) run concurrently.


Creating Threads

The System.Threading.Thread class offers all the methods and properties you need to create and manage threads.
To create a new thread you initiate a new Thread object and then invoke its Start method.
A Thread constructor requires one argument, a ThreadStart delegate object that points to the routine that runs when the thread starts.
Such a routine must be a Sub with no arguments.


System.Diagnostics.GetCurrentProcess(); 

Gets a new process component and associates it with the currently active process


.MainWindowHandle 

gets the window handle of the main window of the associated/active process



Links

link - aspalliance.com/633
link - developerfusion.co.uk/show/4472/2/
link - msdn2.microsoft.com/en-us/library/tttdef8x.aspx
link - albahari.com/threading/#_Creating_and_Starting_Threads


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext