Udostępnij za pośrednictwem


How to: Create Threads

The BackgroundWorker component replaces and adds functionality to the System.Threading namespace; however, the System.Threading namespace is retained for both backward compatibility and future use, if you choose. For more information, see BackgroundWorker Component Overview.

With the .NET Framework, you can 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 information about the techniques involved in working with multiple threads, see Thread-Safe Components.

To create a new thread of execution

  1. Declare the thread.

    Dim myThread as System.Threading.Thread
    
    System.Threading.Thread myThread;
    
  2. Create an instance of 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 in Visual C#.

    myThread = New System.Threading.Thread(AddressOf myStartingMethod)
    
    myThread = new System.Threading.Thread(new 
       System.Threading.ThreadStart(myStartingMethod));
    
  3. When ready, call the Thread.Start method to start the thread.

    myThread.Start()
    
    myThread.Start();
    

See Also

Tasks

How to: Coordinate Multiple Threads of Execution

Reference

BackgroundWorker Component Overview

Concepts

Thread-Safe Components

Event-based Asynchronous Pattern Overview

Other Resources

Multithreading in Components