ThreadStart 대리자

정의

Thread에서 실행되는 메서드를 나타냅니다.Represents the method that executes on a Thread.

public delegate void ThreadStart();
public delegate void ThreadStart();
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
type ThreadStart = delegate of unit -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadStart = delegate of unit -> unit
Public Delegate Sub ThreadStart()
상속
ThreadStart
특성

예제

다음 코드 예제에는 만들기 및 사용에 대 한 구문을 보여 줍니다는 ThreadStart 인스턴스 메서드를 사용 하 여 및 정적 메서드로 위임 합니다.The following code example shows the syntax for creating and using a ThreadStart delegate with an instance method and with a static method.

만드는 방법을 보여 주는 다른 간단한 예제는 ThreadStart 대리자를 참조 하세요는 Thread.Start() 메서드 오버 로드 합니다.For another simple example that demonstrates how to create a ThreadStart delegate, see the Thread.Start() method overload. 스레드 만들기에 대 한 자세한 내용은 참조 하세요. 스레드 만들기 및 시작 시 데이터 전달합니다.For more information about thread creation, see Creating Threads and Passing Data at Start Time.

using namespace System;
using namespace System::Threading;
ref class Work
{
public:
   static void DoWork()
   {
      Console::WriteLine( "Static thread procedure." );
   }

   int Data;
   void DoMoreWork()
   {
      Console::WriteLine( "Instance thread procedure. Data={0}", Data );
   }

};

int main()
{
   
   // To start a thread using an instance method for the thread 
   // procedure, specify the object as the first argument of the
   // ThreadStart constructor.
   //
   Work^ w = gcnew Work;
   w->Data = 42;
   ThreadStart^ threadDelegate = gcnew ThreadStart( w, &Work::DoMoreWork );
   Thread^ newThread = gcnew Thread( threadDelegate );
   newThread->Start();
   
   // To start a thread using a static thread procedure, specify
   // only the address of the procedure. This is a change from 
   // earlier versions of the .NET Framework, which required 
   // two arguments, the first of which was null (0).
   //
   threadDelegate = gcnew ThreadStart( &Work::DoWork );
   newThread = gcnew Thread( threadDelegate );
   newThread->Start();
}

/* This code example produces the following output (the order 
   of the lines might vary):
Static thread procedure.
Instance thread procedure. Data=42
 */
using System;
using System.Threading;

class Test
{
    static void Main() 
    {
        // To start a thread using a static thread procedure, use the
        // class name and method name when you create the ThreadStart
        // delegate. Beginning in version 2.0 of the .NET Framework,
        // it is not necessary to create a delegate explicitly. 
        // Specify the name of the method in the Thread constructor, 
        // and the compiler selects the correct delegate. For example:
        //
        // Thread newThread = new Thread(Work.DoWork);
        //
        ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
        Thread newThread = new Thread(threadDelegate);
        newThread.Start();

        // To start a thread using an instance method for the thread 
        // procedure, use the instance variable and method name when 
        // you create the ThreadStart delegate. Beginning in version
        // 2.0 of the .NET Framework, the explicit delegate is not
        // required.
        //
        Work w = new Work();
        w.Data = 42;
        threadDelegate = new ThreadStart(w.DoMoreWork);
        newThread = new Thread(threadDelegate);
        newThread.Start();
    }
}

class Work 
{
    public static void DoWork() 
    {
        Console.WriteLine("Static thread procedure."); 
    }
    public int Data;
    public void DoMoreWork() 
    {
        Console.WriteLine("Instance thread procedure. Data={0}", Data); 
    }
}

/* This code example produces the following output (the order 
   of the lines might vary):
Static thread procedure.
Instance thread procedure. Data=42
 */
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        ' To start a thread using a static thread procedure, use the
        ' class name and method name when you create the ThreadStart
        ' delegate. Visual Basic expands the AddressOf expression 
        ' to the appropriate delegate creation syntax:
        '    New ThreadStart(AddressOf Work.DoWork)
        '
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start()

        ' To start a thread using an instance method for the thread 
        ' procedure, use the instance variable and method name when 
        ' you create the ThreadStart delegate. Visual Basic expands 
        ' the AddressOf expression to the appropriate delegate 
        ' creation syntax:
        '    New ThreadStart(AddressOf w.DoMoreWork)
        '
        Dim w As New Work()
        w.Data = 42
        newThread = new Thread(AddressOf w.DoMoreWork)
        newThread.Start()
    End Sub
End Class

Public Class Work 
    Public Shared Sub DoWork()
        Console.WriteLine("Static thread procedure.")
    End Sub
    Public Data As Integer
    Public Sub DoMoreWork() 
        Console.WriteLine("Instance thread procedure. Data={0}", Data) 
    End Sub
End Class

' This code example produces the following output (the order 
'   of the lines might vary):
'
'Static thread procedure.
'Instance thread procedure. Data=42

설명

스레드에서 실행 되는 메서드는 나타내는 관리 되는 스레드를 만들면를 ThreadStart 대리자 또는 ParameterizedThreadStart 에 전달 되는 대리자는 Thread 생성자입니다.When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate or a ParameterizedThreadStart delegate that is passed to the Thread constructor. 스레드가 실행 될 때까지 시작 하지는 Thread.Start 메서드가 호출 됩니다.The thread does not begin executing until the Thread.Start method is called. 나타내는 메서드의 첫 번째 줄에서 실행을 시작 합니다 ThreadStart 또는 ParameterizedThreadStart 대리자입니다.Execution begins at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate.

참고

Visual Basic 및 C# 사용자를 생략할 수는 ThreadStart 또는 ParameterizedThreadStart 스레드를 만드는 경우 대리자 생성자입니다.Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. Visual Basic에서 사용 하 여는 AddressOf 메서드를 전달 하는 경우 연산자는 Thread 생성자; 예를 들어 Dim t As New Thread(AddressOf ThreadProc)합니다.In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). C#에서 스레드 프로시저의 이름을 지정 하면 됩니다.In C#, simply specify the name of the thread procedure. 컴파일러는 올바른 대리자 생성자를 선택 합니다.The compiler selects the correct delegate constructor.

C + +의.NET Framework 2.0부터, 만들기를 ThreadStart 정적 메서드 매개 변수가 하나만 필요한 대리자: 콜백 메서드, 클래스 이름으로 정규화 된 주소입니다.For C++, starting with .NET Framework 2.0, creating a ThreadStart delegate for a static method requires only one parameter: the address of the callback method, qualified by the class name. 이전 버전에서는 두 개의 매개 변수가 필요 했습니다 정적 메서드에 대 한 대리자를 만드는 경우: 0 (null) 및 메서드 주소입니다.In earlier versions two parameters were required when creating a delegate for a static method: zero (null) and the method address. 인스턴스 메서드에 대 한 모든 버전에 두 매개 변수가 필요 합니다: 인스턴스 변수 및 메서드 주소입니다.For an instance method, all versions require two parameters: the instance variable and the method address.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.Gets an object that represents the method represented by the specified delegate.

적용 대상

추가 정보