The .NET Framework allows you to create multithreaded applications quickly and easily by providing an object-oriented threading model. Creating a new thread is as easy as declaring it and supplying it with a delegate to the method where the thread is to start. When you are ready to begin execution on the thread, call the Thread.Start Method. There are special considerations involved when working with multiple threads of execution. For an overview of techniques involved in working with multiple threads, see Thread-Safe Components.
To create a new thread of execution
- Declare the thread.
' Visual Basic Dim myThread as System.Threading.Thread // C# System.Threading.Thread myThread;
- Instantiate the thread with the appropriate delegate for the starting point of the thread. Use the AddressOf operator to create the delegate in Visual Basic, or create a new ThreadStart object in C#.
' Visual Basic myThread = New System.Threading.Thread(AddressOf myStartingMethod) // C# myThread = new System.Threading.Thread(new System.Threading.ThreadStart(myStartingMethod));
- When ready, call the Thread.Start method to start the thread.
' Visual Basic myThread.Start() // C# myThread.Start();