Export (0) Print
Expand All

ThreadStart Delegate

Represents the method that executes on the Thread.

[Visual Basic]
<Serializable>
Public Delegate Sub ThreadStart()
[C#]
[Serializable]
public delegate void ThreadStart();
[C++]
[Serializable]
public __gc __delegate void ThreadStart();

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your callback method must have the same parameters as the ThreadStart delegate declaration.

Remarks

When a thread is created, the new instance of the Thread class is created using a constructor that takes the ThreadStart delegate as its only parameter. However, the thread does not begin executing until the Start method is invoked. When Start is called, execution begins at the first line of the method referenced by the ThreadStart delegate.

[Visual Basic] Note   Visual Basic users can omit the ThreadStart constructor when creating a thread. Use the AddressOf operator when passing your method to the Thread constructor, for example Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically calls the ThreadStart constructor.

Example

[Visual Basic, C#, C++] The following code example shows the syntax for creating and using a ThreadStart delegate.

[Visual Basic] 
Imports System
Imports System.Threading

Public Class Test
    Shared Sub Main()
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start()
    End Sub
End Class

Public Class Work 
    Shared Sub DoWork()
    End Sub
End Class

[C#] 
using System;
using System.Threading;

class Test
{
    static void Main() 
    {
        ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
        Thread newThread = new Thread(threadDelegate);
        newThread.Start();
    }
}

class Work 
{
    public static void DoWork() {}
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class Work 
{
public:
    static void DoWork() {}
};

void main() 
{
    ThreadStart* threadDelegate = new ThreadStart(0, &Work::DoWork);
    Thread* newThread = new Thread(threadDelegate);
    newThread->Start();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Threading Namespace | Thread | AppDomain | Creating Threads

Show:
© 2014 Microsoft