| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Creating Threads and Passing Data at Start Time |
03/30/2017 |
.net |
dotnet-standard |
article |
|
52b32222-e185-4f42-91a7-eaca65c0ab6d |
18 |
rpetrusha |
ronpet |
wpickett |
Creating Threads and Passing Data at Start Time
When an operating-system process is created, the operating system injects a thread to execute code in that process, including any original application domain. From that point on, application domains can be created and destroyed without any operating system threads necessarily being created or destroyed. If the code being executed is managed code, then a xref:System.Threading.Thread object for the thread executing in the current application domain can be obtained by retrieving the static xref:System.Threading.Thread.CurrentThread%2A property of type xref:System.Threading.Thread. This topic describes thread creation and discusses alternatives for passing data to the thread procedure.
Creating a Thread
Creating a new xref:System.Threading.Thread object creates a new managed thread. The xref:System.Threading.Thread class has constructors that take a xref:System.Threading.ThreadStart delegate or a xref:System.Threading.ParameterizedThreadStart delegate; the delegate wraps the method that is invoked by the new thread when you call the xref:System.Threading.Thread.Start%2A method. Calling xref:System.Threading.Thread.Start%2A more than once causes a xref:System.Threading.ThreadStateException to be thrown.
The xref:System.Threading.Thread.Start%2A method returns immediately, often before the new thread has actually started. You can use the xref:System.Threading.Thread.ThreadState%2A and xref:System.Threading.Thread.IsAlive%2A properties to determine the state of the thread at any one moment, but these properties should never be used for synchronizing the activities of threads.
[!NOTE] Once a thread is started, it is not necessary to retain a reference to the xref:System.Threading.Thread object. The thread continues to execute until the thread procedure ends.
The following code example creates two new threads to call instance and static methods on another object.
[!code-cppSystem.Threading.ThreadStart2#2] [!code-csharpSystem.Threading.ThreadStart2#2] [!code-vbSystem.Threading.ThreadStart2#2]
Passing Data to Threads and Retrieving Data from Threads
In the .NET Framework version 2.0, the xref:System.Threading.ParameterizedThreadStart delegate provides an easy way to pass an object containing data to a thread when you call the xref:System.Threading.Thread.Start%2A?displayProperty=nameWithType method overload. See xref:System.Threading.ParameterizedThreadStart for a code example.
Using the xref:System.Threading.ParameterizedThreadStart delegate is not a type-safe way to pass data, because the xref:System.Threading.Thread.Start%2A?displayProperty=nameWithType method overload accepts any object. An alternative is to encapsulate the thread procedure and the data in a helper class and use the xref:System.Threading.ThreadStart delegate to execute the thread procedure. This technique is shown in the two code examples that follow.
Neither of these delegates has a return value, because there is no place to return the data from an asynchronous call. To retrieve the results of a thread method, you can use a callback method, as demonstrated in the second code example.
[!code-cppSystem.Threading.ThreadStart2#3] [!code-csharpSystem.Threading.ThreadStart2#3] [!code-vbSystem.Threading.ThreadStart2#3]
Retrieving Data with Callback Methods
The following example demonstrates a callback method that retrieves data from a thread. The constructor for the class that contains the data and the thread method also accepts a delegate representing the callback method; before the thread method ends, it invokes the callback delegate.
[!code-cppSystem.Threading.ThreadStart2#4] [!code-csharpSystem.Threading.ThreadStart2#4] [!code-vbSystem.Threading.ThreadStart2#4]
See Also
xref:System.Threading.Thread
xref:System.Threading.ThreadStart
xref:System.Threading.ParameterizedThreadStart
xref:System.Threading.Thread.Start%2A?displayProperty=nameWithType
Threading
Using Threads and Threading