다음을 통해 공유


ThreadStart 대리자

Thread에서 실행되는 메서드를 나타냅니다.

네임스페이스: System.Threading
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public Delegate Sub ThreadStart ()
‘사용 방법
Dim instance As New ThreadStart(AddressOf HandlerMethod)
[ComVisibleAttribute(true)] 
public delegate void ThreadStart ()
[ComVisibleAttribute(true)] 
public delegate void ThreadStart ()
/** @delegate */
/** @attribute ComVisibleAttribute(true) */ 
public delegate void ThreadStart ()
JScript에서는 대리자를 사용할 수 있지만 새로 선언할 수는 없습니다.

설명

관리되는 스레드가 만들어지면 스레드에서 실행되는 메서드가 ThreadStart 대리자나 Thread 생성자에 전달되는 ParameterizedThreadStart 대리자로 나타납니다. 스레드는 System.Threading.Thread.Start 메서드가 호출될 때까지 실행을 시작하지 않습니다. 실행은 ThreadStart 또는 ParameterizedThreadStart 대리자로 나타나는 메서드의 첫 줄에서 시작됩니다.

참고

Visual Basic 및 C# 사용자는 스레드를 만들 때 ThreadStart 또는 ParameterizedThreadStart 대리 생성자를 생략할 수 있습니다. Visual Basic에서는 Thread 생성자에 메서드를 전달할 때 AddressOf 연산자를 사용합니다(예: Dim t As New Thread(AddressOf ThreadProc)). C#에서는 스레드 프로시저의 이름만 지정하면 되고 컴파일러에서 올바른 대리 생성자를 선택합니다.

참고

.NET Framework 버전 2.0에서는 C++로 정적 메서드의 ThreadStart 대리자를 만들 때 클래스 이름으로 정규화된 콜백 메서드의 주소만 매개 변수로 필요합니다. 이전 버전에서는 정적 메서드의 대리자를 만들 때 0(null)과 메서드 주소가 매개 변수로 필요했습니다. 인스턴스 메서드의 경우 모든 버전에서 인스턴스 변수와 메서드 주소가 매개 변수로 필요합니다.

예제

다음 코드 예제에서는 인스턴스 메서드와 정적 메서드를 사용하여 ThreadStart 대리자를 만들고 사용하는 구문을 보여 줍니다.

ThreadStart 대리자를 만드는 방법을 보여주는 다른 간단한 예제를 보려면 Thread.Start 메서드 오버로드를 참조하십시오. 스레드 작성에 대한 자세한 내용은 스레드 만들기 및 시작할 때 데이터 전달을 참조하십시오.

Imports System
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
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 explicityly. 
        // 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
 */
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
 */
import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    public static void main(String[] args)
    {
        ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
        Thread newThread = new Thread(threadDelegate);
        newThread.Start();
    } //main
} //Test

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

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

System.Threading 네임스페이스
ParameterizedThreadStart 대리자
Thread 클래스
Start
AppDomain

기타 리소스

스레드 만들기 및 시작할 때 데이터 전달