ThreadStart デリゲートとは? わかりやすく解説

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 では、デリゲート使用できますが、新規に宣言することはできません。
解説解説

マネージ スレッド作成すると、そのスレッド実行するメソッドが、Thread コンストラクタ渡される ThreadStart デリゲートまたは ParameterizedThreadStart デリゲート表されます。スレッドは System.Threading.Thread.Start メソッド呼び出されるまで実行開始しません。実行は、ThreadStart または ParameterizedThreadStart デリゲート表されるメソッド最初の行から開始されます。

メモメモ

Visual Basic および C#ユーザーは、スレッド作成するときに ThreadStart または ParameterizedThreadStart デリゲート コンストラクタ省略できますVisual Basic では、メソッドThread コンストラクタに渡すときに、Dim t As New Thread(AddressOf ThreadProc) のように AddressOf 演算子使用しますC# では、単純にスレッド プロシージャの名前を指定しますコンパイラにより、適切なデリゲート コンストラクタ選択されます。

使用例使用例

インスタンス メソッドおよび静的メソッド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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ThreadStart デリゲート」の関連用語

ThreadStart デリゲートのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ThreadStart デリゲートのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS