Thread 생성자

정의

Thread 클래스의 새 인스턴스를 초기화합니다.

오버로드

Thread(ParameterizedThreadStart)

스레드가 시작될 때 개체가 스레드로 전달될 수 있도록 하는 대리자를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

Thread(ThreadStart)

Thread 클래스의 새 인스턴스를 초기화합니다.

Thread(ParameterizedThreadStart, Int32)

스레드가 시작될 때 스레드로 개체가 전달될 수 있도록 하는 대리자를 지정하고 스레드의 최대 스택 크기를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

Thread(ThreadStart, Int32)

스레드의 최대 스택 크기를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

Thread(ParameterizedThreadStart)

스레드가 시작될 때 개체가 스레드로 전달될 수 있도록 하는 대리자를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

public:
 Thread(System::Threading::ParameterizedThreadStart ^ start);
public Thread (System.Threading.ParameterizedThreadStart start);
new System.Threading.Thread : System.Threading.ParameterizedThreadStart -> System.Threading.Thread
Public Sub New (start As ParameterizedThreadStart)

매개 변수

start
ParameterizedThreadStart

이 스레드의 실행이 시작될 때 호출될 메서드를 나타내는 대리자입니다.

예외

startnull입니다.

예제

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

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
            // Start a thread that calls a parameterized static method.
            Thread^ newThread = gcnew
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
            newThread->Start(42);
              
            // Start a thread that calls a parameterized instance method.
            Work^ someWork = gcnew Work;
            newThread = gcnew Thread(
                        gcnew ParameterizedThreadStart(someWork,
                        &Work::DoMoreWork));
            newThread->Start("The answer.");
        }

        static void DoWork(Object^ data)
        {
            Console::WriteLine("Static thread procedure. Data='{0}'", 
                data);
        }

        void DoMoreWork(Object^ data)
        {
            Console::WriteLine("Instance thread procedure. Data='{0}'", 
                data);
        }
    };
}

//Entry point of example application
int main()
{
    SystemThreadingExample::Work^ samplework = 
        gcnew SystemThreadingExample::Work();
    samplework->StartThreads();
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        newThread.Start("The answer.");
    }
 
    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
Imports System.Threading

Public Class Work
    Shared Sub Main()
        ' Start a thread that calls a parameterized static method.
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start(42)

        ' Start a thread that calls a parameterized instance method.
        Dim w As New Work()
        newThread = New Thread(AddressOf w.DoMoreWork)
        newThread.Start("The answer.")
    End Sub
 
    Public Shared Sub DoWork(ByVal data As Object)
        Console.WriteLine("Static thread procedure. Data='{0}'",
                          data)
    End Sub

    Public Sub DoMoreWork(ByVal data As Object) 
        Console.WriteLine("Instance thread procedure. Data='{0}'",
                          data)
    End Sub
End Class
' This example displays output like the following:
'    Static thread procedure. Data='42'
'    Instance thread procedure. Data='The answer.'

설명

스레드가 생성 될 때 실행을 시작 하지 않습니다. 스레드 실행을 예약 하려면 메서드를 호출 Start 합니다. 데이터 개체를 스레드에 전달 하려면 Start(Object) 메서드 오버 로드를 사용 합니다.

참고

Visual Basic 사용자는 ThreadStart 스레드를 만들 때 생성자를 생략할 수 있습니다. AddressOf메서드를 전달할 때 연산자를 사용 합니다 (예:) Dim t As New Thread(AddressOf ThreadProc) . Visual Basic ThreadStart 자동으로 생성자를 호출합니다.

추가 정보

적용 대상

Thread(ThreadStart)

Thread 클래스의 새 인스턴스를 초기화합니다.

public:
 Thread(System::Threading::ThreadStart ^ start);
public Thread (System.Threading.ThreadStart start);
new System.Threading.Thread : System.Threading.ThreadStart -> System.Threading.Thread
Public Sub New (start As ThreadStart)

매개 변수

start
ThreadStart

이 스레드의 실행이 시작될 때 호출될 메서드를 나타내는 ThreadStart 대리자입니다.

예외

start 매개 변수가 null인 경우

예제

다음 코드 예제에서는 정적 메서드를 실행하는 스레드를 만드는 방법을 보여줍니다.

using namespace System;
using namespace System::Threading;
ref class Work
{
private:
   Work(){}


public:
   static void DoWork(){}

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &Work::DoWork ) );
   newThread->Start();
}
using System;
using System.Threading;

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

class Work 
{
    Work() {}

    public static void DoWork() {}
}
Imports System.Threading

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

Public Class Work 

    Private Sub New()
    End Sub

    Shared Sub DoWork()
    End Sub

End Class

다음 코드 예제에서는 인스턴스 메서드를 실행하는 스레드를 만드는 방법을 보여줍니다.

using namespace System;
using namespace System::Threading;
ref class Work
{
public:
   Work(){}

   void DoWork(){}

};

int main()
{
   Work^ threadWork = gcnew Work;
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( threadWork, &Work::DoWork ) );
   newThread->Start();
}
using System;
using System.Threading;

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

class Work 
{
    public Work() {}

    public void DoWork() {}
}
Imports System.Threading

Public Class Test
    <MTAThread> _
    Shared Sub Main() 
        Dim threadWork As New Work()
        Dim newThread As New Thread(AddressOf threadWork.DoWork)
        newThread.Start()
    End Sub
End Class

Public Class Work

    Sub New()
    End Sub

    Sub DoWork() 
    End Sub

End Class

설명

스레드를 만들 때 실행이 시작되지 않습니다. 스레드 실행을 예약하려면 Start 메서드를 호출합니다.

참고

Visual Basic 사용자는 ThreadStart 스레드를 만들 때 생성자를 생략할 수 있습니다. AddressOf메서드를 전달할 때 연산자를 Dim t As New Thread(AddressOf ThreadProc) 사용합니다(예: ). Visual Basic ThreadStart 자동으로 생성자를 호출합니다.

추가 정보

적용 대상

Thread(ParameterizedThreadStart, Int32)

스레드가 시작될 때 스레드로 개체가 전달될 수 있도록 하는 대리자를 지정하고 스레드의 최대 스택 크기를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

public:
 Thread(System::Threading::ParameterizedThreadStart ^ start, int maxStackSize);
public Thread (System.Threading.ParameterizedThreadStart start, int maxStackSize);
new System.Threading.Thread : System.Threading.ParameterizedThreadStart * int -> System.Threading.Thread
Public Sub New (start As ParameterizedThreadStart, maxStackSize As Integer)

매개 변수

start
ParameterizedThreadStart

이 스레드의 실행이 시작될 때 호출될 메서드를 나타내는 ParameterizedThreadStart 대리자입니다.

maxStackSize
Int32

스레드에서 사용할 최대 스택 크기(바이트)입니다. 실행 파일의 헤더에 지정된 기본 최대 스택 크기를 사용하려면 0을 지정합니다.

중요: 부분적으로 신뢰할 수 있는 코드의 경우 maxStackSize가 기본 스택 크기보다 크면 무시됩니다. 예외가 throw되지 않습니다.

예외

startnull입니다.

maxStackSize가 0보다 작은 경우

설명

이 생성자 오버로드를 사용하지 마십시오. 생성자 오버로드에서 사용하는 기본 스택 Thread(ParameterizedThreadStart) 크기는 스레드에 권장되는 스택 크기입니다. 스레드에 메모리 문제가 있는 경우 가장 가능성이 높은 원인은 무한 재귀와 같은 프로그래밍 오류입니다.

중요

.NET Framework 4부터 완전히 신뢰할 수 있는 코드만 maxStackSize 기본 스택 크기(1MB)보다 큰 값으로 설정할 수 있습니다. 코드가 부분 신뢰로 실행될 때 에 대해 더 큰 값을 지정하면 maxStackSize maxStackSize 가 무시되고 기본 스택 크기가 사용됩니다. 예외는 throw되지 않습니다. 모든 신뢰 수준의 maxStackSize 코드는 기본 스택 크기보다 작은 값으로 설정할 수 있습니다.

참고

부분적으로 신뢰할 수 있는 코드에서 사용할 완전히 신뢰할 수 있는 라이브러리를 개발하고 큰 스택이 필요한 스레드를 시작해야 하는 경우 스레드를 만들기 전에 완전 신뢰를 어설션해야 합니다. 그렇지 않으면 기본 스택 크기가 사용됩니다. 스레드에서 실행되는 코드를 완전히 제어하지 않는 한 이 작업을 수행하지 마십시오.

가 최소 스택 크기보다 작은 경우 maxStackSize 최소 스택 크기가 사용됩니다. 가 페이지 크기의 배수가 아닌 경우 maxStackSize 페이지 크기의 다음으로 큰 배수로 반올림됩니다. 예를 들어 Windows Vista에서 .NET Framework 버전 2.0을 사용하는 경우 최소 스택 크기는 256KB(262,144바이트)이고 페이지 크기는 64KB(65,536바이트)입니다.

참고

WINDOWS XP 및 Windows Server 2003 이전 버전의 Microsoft Windows maxStackSize 에서는 가 무시되고 실행 파일 헤더에 지정된 스택 크기가 사용됩니다.

매우 작은 스택 크기를 지정하는 경우 스택 오버플로 검색을 사용하지 않도록 설정해야 할 수 있습니다. 스택이 심각하게 제한되면 검색 자체에서 스택 오버플로가 발생할 수 있습니다. 스택 오버플로 검색을 사용 하지 않으려면 다음 애플리케이션 구성 파일을 추가 합니다.

<configuration>  
  <runtime>  
    <disableStackOverflowProbing enabled="true"/>  
  </runtime>  
</configuration>  

적용 대상

Thread(ThreadStart, Int32)

스레드의 최대 스택 크기를 지정하여 Thread 클래스의 새 인스턴스를 초기화합니다.

public:
 Thread(System::Threading::ThreadStart ^ start, int maxStackSize);
public Thread (System.Threading.ThreadStart start, int maxStackSize);
new System.Threading.Thread : System.Threading.ThreadStart * int -> System.Threading.Thread
Public Sub New (start As ThreadStart, maxStackSize As Integer)

매개 변수

start
ThreadStart

이 스레드의 실행이 시작될 때 호출될 메서드를 나타내는 ThreadStart 대리자입니다.

maxStackSize
Int32

스레드에서 사용할 최대 스택 크기(바이트)입니다. 실행 파일의 헤더에 지정된 기본 최대 스택 크기를 사용하려면 0을 지정합니다.

중요: 부분적으로 신뢰할 수 있는 코드의 경우 maxStackSize가 기본 스택 크기보다 크면 무시됩니다. 예외가 throw되지 않습니다.

예외

startnull입니다.

maxStackSize가 0보다 작은 경우

설명

이 생성자 오버로드를 사용하지 마십시오. 생성자 오버로드에서 사용하는 기본 스택 Thread(ThreadStart) 크기는 스레드에 권장되는 스택 크기입니다. 스레드에 메모리 문제가 있는 경우 가장 가능성이 높은 원인은 무한 재귀와 같은 프로그래밍 오류입니다.

중요

.NET Framework 4부터 완전히 신뢰할 수 있는 코드만 maxStackSize 기본 스택 크기(1MB)보다 큰 값으로 설정할 수 있습니다. 코드가 부분 신뢰로 실행될 때 에 대해 더 큰 값을 지정하면 maxStackSize maxStackSize 가 무시되고 기본 스택 크기가 사용됩니다. 예외는 throw되지 않습니다. 모든 신뢰 수준의 maxStackSize 코드는 기본 스택 크기보다 작은 값으로 설정할 수 있습니다.

참고

부분적으로 신뢰할 수 있는 코드에서 사용할 완전히 신뢰할 수 있는 라이브러리를 개발하고 큰 스택이 필요한 스레드를 시작해야 하는 경우 스레드를 만들기 전에 완전 신뢰를 어설션해야 합니다. 그렇지 않으면 기본 스택 크기가 사용됩니다. 스레드에서 실행되는 코드를 완전히 제어하지 않는 한 이 작업을 수행하지 마십시오.

가 최소 스택 크기보다 작은 경우 maxStackSize 최소 스택 크기가 사용됩니다. 가 페이지 크기의 배수가 아닌 경우 maxStackSize 페이지 크기의 다음으로 큰 배수로 반올림됩니다. 예를 들어 Windows Vista에서 .NET Framework 버전 2.0을 사용하는 경우 최소 스택 크기는 256KB(262,144바이트)이고 페이지 크기는 64KB(65,536바이트)입니다.

참고

WINDOWS XP 및 Windows Server 2003 이전 버전의 Microsoft Windows maxStackSize 에서는 가 무시되고 실행 파일 헤더에 지정된 스택 크기가 사용됩니다.

매우 작은 스택 크기를 지정하는 경우 스택 오버플로 검색을 사용하지 않도록 설정해야 할 수 있습니다. 스택이 심각하게 제한되면 검색 자체에서 스택 오버플로가 발생할 수 있습니다. 스택 오버플로 검색을 사용 하지 않으려면 다음 애플리케이션 구성 파일을 추가 합니다.

<configuration>  
  <runtime>  
    <disableStackOverflowProbing enabled="true"/>  
  </runtime>  
</configuration>  

적용 대상