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.

Imports 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 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++)
         {
             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();
   }
}



C# useful website on threading - http://www.albahari.com/threading/


Interlock class ?



The .NET framework fully supports multithreaded applications.


Preemptive 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 iwn 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.


Links

- Create a Threaded NNTP News Reader
- Getting the list of groups in a news server
- Handling basic NNTP commands
- An Asynchronous Socket Server and Client
- How to NNTP in C# - Connecting & Disconnecting
- Get the client's IP address using TcpClient in VB.NET
- Pausing and Resuming Threads


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



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

Next