-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathParameterizedThreadStart.xml
107 lines (90 loc) · 9.02 KB
/
ParameterizedThreadStart.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<Type Name="ParameterizedThreadStart" FullName="System.Threading.ParameterizedThreadStart">
<TypeSignature Language="C#" Value="public delegate void ParameterizedThreadStart(object? obj);" FrameworkAlternate="net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-3.0;netcore-3.1" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed ParameterizedThreadStart extends System.MulticastDelegate" />
<TypeSignature Language="DocId" Value="T:System.Threading.ParameterizedThreadStart" />
<TypeSignature Language="VB.NET" Value="Public Delegate Sub ParameterizedThreadStart(obj As Object)" />
<TypeSignature Language="F#" Value="type ParameterizedThreadStart = delegate of obj -> unit" />
<TypeSignature Language="C++ CLI" Value="public delegate void ParameterizedThreadStart(System::Object ^ obj);" />
<TypeSignature Language="C#" Value="public delegate void ParameterizedThreadStart(object obj);" FrameworkAlternate="netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-2.0;netstandard-2.1" />
<AssemblyInfo>
<AssemblyName>System.Threading.Thread</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeForwardingChain>
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="10.0.0.0" FrameworkAlternate="net-10.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="5.0.0.0" FrameworkAlternate="net-5.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="6.0.0.0" FrameworkAlternate="net-6.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="7.0.0.0" FrameworkAlternate="net-7.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="8.0.0.0" FrameworkAlternate="net-8.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Threading.Thread" ToVersion="9.0.0.0" FrameworkAlternate="net-9.0" />
</TypeForwardingChain>
<Base>
<BaseTypeName>System.Delegate</BaseTypeName>
</Base>
<Attributes>
<Attribute FrameworkAlternate="netcore-1.0;netcore-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Runtime.InteropServices.ComVisible(false)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.InteropServices.ComVisible(false)>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="obj" Type="System.Object" />
</Parameters>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Docs>
<param name="obj">An object that contains data for the thread procedure.</param>
<summary>Represents the method that executes on a <see cref="T:System.Threading.Thread" />.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When a managed thread is created, the method that executes on the thread is represented by:
- A <xref:System.Threading.ThreadStart> delegate that is passed to the <xref:System.Threading.Thread.%23ctor%28System.Threading.ThreadStart%29?displayProperty=nameWithType> constructor. Any method that has no parameters and that returns `void` in C# or is a `Sub` procedure in Visual Basic can represent the delegate.
- A <xref:System.Threading.ParameterizedThreadStart> delegate that is passed to the <xref:System.Threading.Thread.%23ctor%28System.Threading.ParameterizedThreadStart%29?displayProperty=nameWithType> constructor. Any method that has a single parameter of type <xref:System.Object> and that returns void in C# or is a Sub procedure in Visual Basic can represent the delegate.
The thread does not begin executing until the <xref:System.Threading.Thread.Start%2A?displayProperty=nameWithType> method is called. The <xref:System.Threading.ThreadStart> or <xref:System.Threading.ParameterizedThreadStart> delegate is invoked on the thread, and execution begins at the first line of the method represented by the delegate. In the case of the <xref:System.Threading.ParameterizedThreadStart> delegate, the object that is passed to the <xref:System.Threading.Thread.Start%28System.Object%29> method is passed to the delegate.
> [!NOTE]
> Visual Basic and C# users can omit the <xref:System.Threading.ThreadStart> or <xref:System.Threading.ParameterizedThreadStart> delegate constructor when creating a thread. In Visual Basic, use the `AddressOf` operator when passing your method to the <xref:System.Threading.Thread.%23ctor%2A> constructor; for example, `Dim t As New Thread(AddressOf ThreadProc)`. In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor.
> [!NOTE]
> When you create a <xref:System.Threading.ParameterizedThreadStart> delegate for an instance method in C++, the first parameter of the constructor is the instance variable. For a static method, the first parameter of the constructor is zero. For a static method, the delegate constructor requires only one parameter: the address of the callback method, qualified by the class name.
The <xref:System.Threading.ParameterizedThreadStart> delegate and the <xref:System.Threading.Thread.Start%28System.Object%29?displayProperty=nameWithType> method overload make it easy to pass data to a thread procedure, but this technique is not type safe because any object can be passed to <xref:System.Threading.Thread.Start%28System.Object%29?displayProperty=nameWithType>. A more robust way to pass data to a thread procedure is to put both the thread procedure and the data fields into a worker object. For more information, see [Creating Threads and Passing Data at Start Time](/dotnet/standard/threading/creating-threads-and-passing-data-at-start-time).
The <xref:System.Threading.ParameterizedThreadStart> delegate supports only a single parameter. You can pass multiple data items to the <xref:System.Threading.ParameterizedThreadStart> by making that parameter one of the following:
- An array.
- A collection type, if all of the data items are of the same type.
- A tuple type, such as <xref:System.Tuple%602> or <xref:System.Tuple%604>.
## Examples
The following code example uses a <xref:System.Threading.ParameterizedThreadStart> delegate to execute a static method and an instance method. The first <xref:System.Threading.ParameterizedThreadStart> delegate is represented by the static `DoWork` method and the second is represented by the instance `DoMoreWork` method. Both methods match the <xref:System.Threading.ParameterizedThreadStart> delegate signature; that is, they have a single parameter of type <xref:System.Object> and don't return a value.
> [!NOTE]
> The Visual Basic and C# compilers infer the <xref:System.Threading.ParameterizedThreadStart> delegate from the signatures of the `DoWork` and `DoMoreWork` methods, and call the correct constructor. Thus, there is no explicit constructor call in the code.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Threading.ParameterizedThreadStart/CPP/source.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Threading/ParameterizedThreadStart/Overview/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Threading.ParameterizedThreadStart/VB/source.vb" id="Snippet1":::
]]></format>
</remarks>
<altmember cref="T:System.Threading.ThreadStart" />
<altmember cref="T:System.Threading.Thread" />
<altmember cref="Overload:System.Threading.Thread.Start" />
<altmember cref="T:System.AppDomain" />
<related type="Article" href="/dotnet/standard/threading/creating-threads-and-passing-data-at-start-time">Creating Threads</related>
</Docs>
</Type>