Thread.Start メソッド ()とは? わかりやすく解説

Thread.Start メソッド ()

オペレーティング システムによって、現在のインスタンスの状態を ThreadState.Running に変更します

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ThreadStateException

スレッドが既に起動してます。

SecurityException

呼び出し元に適切な SecurityPermission がありません。

OutOfMemoryException

メモリ不足しているため、このスレッド起動できません。

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

スレッドThreadState.Running 状態になると、オペレーティング システムによってスレッド実行スケジュールされますスレッドは、スレッド コンストラクタ提供された ThreadStart デリゲートまたは ParameterizedThreadStart デリゲートによって表されるメソッド最初の行の実行開始します

いったんスレッド終了すると、Start のための別の呼び出し使って再起動することはできません。

使用例使用例

スレッド作成して起動する例を次に示します

このコードによって、次の出力生成されます。

     In main.
     Working thread...
     In main. Working thread...
     In main.
     Working thread...

この出力ステートメントシーケンス一般的ですが、どのシステムでも同じであることは保証されません。

スレッド プロシージャは、静的メソッドまたはインスタンス メソッドいずれかです。ThreadStart デリゲートに関するコード例参照してくださいスレッド作成詳細については、「スレッド作成し開始時にデータを渡す」を参照してください

Imports System
Imports System.Threading

Public Class ThreadWork
   
   Public Shared Sub DoWork()
      Dim i As Integer
      For i = 0 To 2
         Console.WriteLine("Working thread...")
         Thread.Sleep(100)
      Next i
   End Sub 'DoWork
End Class 'ThreadWork

Class ThreadTest
   
   Public Shared Sub Main()
      Dim myThreadDelegate As New
 ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New
 Thread(myThreadDelegate)
      myThread.Start()
      Dim i As Integer
      For i = 0 To 2
         Console.WriteLine("In main.")
         Thread.Sleep(100)
      Next i
   End Sub 'Main
End Class 'ThreadTest
using System;
using System.Threading;

public class ThreadWork 
{
   public static void DoWork()
   {
      for(int i = 0; i<3;i++)
      {
         Console.WriteLine("Working thread...");
            Thread.Sleep(100);
      }
   }
}
class ThreadTest
{
   public static void Main()
   {
      ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
      Thread myThread = new Thread(myThreadDelegate);
      myThread.Start();
         for(int i = 0; i<3; i++)
         {
         Console.WriteLine("In main.");
            Thread.Sleep(100);
         }
   }
}
using namespace System;
using namespace System::Threading;
public ref class ThreadWork
{
public:
   static void DoWork()
   {
      for ( int i = 0; i < 3; i++ )
      {
         Console::WriteLine( "Working thread..." );
         Thread::Sleep( 100 );

      }
   }

};

int main()
{
   ThreadStart^ myThreadDelegate = gcnew ThreadStart( &ThreadWork::DoWork );
   Thread^ myThread = gcnew Thread( myThreadDelegate );
   myThread->Start();
   for ( int i = 0; i < 3; i++ )
   {
      Console::WriteLine( "In main." );
      Thread::Sleep( 100 );

   }
}

import System.*;
import System.Threading.*;
public class ThreadWork
{
    public static void DoWork()
    {
        for (int i = 0; i < 3; i++) {
            Console.WriteLine("Working thread...");
            System.Threading.Thread.Sleep(100);
        }
    } //DoWork
} //ThreadWork

class ThreadTest
{
    public static void main(String[]
 args)
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        System.Threading.Thread myThread =
            new System.Threading.Thread(myThreadDelegate);
        myThread.Start();
        for (int i = 0; i < 3; i++) {
            Console.WriteLine("In main.");
            System.Threading.Thread.Sleep(100);
        }
    } //main
} //ThreadTest
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Start メソッド (Object)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

オペレーティング システムによって現在のインスタンスの状態が ThreadState.Running に変更されオプションスレッド実行するメソッド使用するデータ格納するオブジェクト提供されます。

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ThreadStateException

スレッドが既に起動してます。

SecurityException

呼び出し元に適切な SecurityPermission がありません。

OutOfMemoryException

メモリ不足しているため、このスレッド起動できません。

InvalidOperationException

このスレッドは、ParameterizedThreadStart デリゲートではなく、ThreadStart デリゲート使用して作成されています。

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

スレッドThreadState.Running 状態になると、オペレーティング システムによってスレッド実行スケジュールされますスレッドは、スレッド コンストラクタ提供されThreadStart デリゲートまたは ParameterizedThreadStart デリゲートによって表されるメソッド最初の行の実行開始します

いったんスレッド終了すると、Start のための別の呼び出し使って再起動することはできません。

このオーバーロードParameterizedThreadStart デリゲート使用すると、データスレッド プロシージャ簡単に渡せるようになります。ただし、この方法では、どのオブジェクトでもこのオーバーロードに渡すことができるため、タイプ セーフではありません。より信頼性の高い方法スレッド プロシージャデータを渡すには、スレッド プロシージャデータ フィールド両方ワーカー オブジェクト格納します詳細については、「スレッド作成し開始時にデータを渡す」を参照してください

使用例使用例

静的メソッドおよびインスタンス メソッドParameterizedThreadStart デリゲート作成および使用するための構文コード例次に示します

Imports System
Imports System.Threading

Public Class Work

    <MTAThread> _
    Shared Sub Main()
        ' To start a thread using a shared thread procedure, use
        ' the class name and method name when you create the 
        ' ParameterizedThreadStart delegate. Visual Basic expands
        ' the AddressOf expression to the appropriate delegate 
        ' creation syntax:
        '    New ParameterizedThreadStart(AddressOf Work.DoWork)
        '
        Dim newThread As New
 Thread(AddressOf Work.DoWork)
        
        ' Use the overload of the Start method that has a
        ' parameter of type Object. You can create an object that
        ' contains several pieces of data, or you can pass any 
        ' object or value type. The following code passes the
        ' integer value 42.
        '
        newThread.Start(42)

        ' To start a thread using an instance method for the thread
 
        ' procedure, use the instance variable and method name when
 
        ' you create the ParameterizedThreadStart delegate. Visual 
        ' Basic expands the AddressOf expression to the appropriate
 
        ' delegate creation syntax:
        '    New ParameterizedThreadStart(AddressOf w.DoMoreWork)
        '
        Dim w As New Work()
        newThread = New Thread(New ParameterizedThreadStart(AddressOf
 w.DoMoreWork))
        'newThread = New Thread(AddressOf w.DoMoreWork)
        
        ' Pass an object containing data for the thread.
        '
        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 code example produces the following output (the order 
'   of the lines might vary):
'
'Static thread procedure. Data='42'
'Instance thread procedure. Data='The answer'
using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use
        // the class name and method name when you create the 
        // ParameterizedThreadStart delegate.
        //
        Thread newThread = new Thread(
            new ParameterizedThreadStart(Work.DoWork));
        
        // Use the overload of the Start method that has a
        // parameter of type Object. You can create an object that
        // contains several pieces of data, or you can pass any 
        // reference type or value type. The following code passes
        // the integer value 42.
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread
 
        // procedure, use the instance variable and method name when
 
        // you create the ParameterizedThreadStart delegate.
        //
        Work w = new Work();
        newThread = new Thread(
            new ParameterizedThreadStart(w.DoMoreWork));
        
        // Pass an object containing data for the thread.
        //
        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 code example produces the following output (the order 
   of the lines might vary):

Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/
using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
              
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            //    AddressOf Work.DoWork)
            //
            Thread^ newThread = gcnew 
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
              
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any
 
            // reference type or value type. The following code passes
            // the integer value 42.
            newThread->Start(42);
              
            // To start a thread using an instance method for the thread
 
            // procedure, use the instance variable and method name when
 
            // you create the ParameterizedThreadStart delegate.
            Work^ someWork = gcnew Work;
            newThread = 
                gcnew Thread(
                gcnew ParameterizedThreadStart(someWork, 
                &Work::DoMoreWork));
              
            // Pass an object containing data for the thread.
            //
            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 code example produces the following output (the order 
//   of the lines might vary):

// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer'

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Start メソッド




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

辞書ショートカット

すべての辞書の索引

「Thread.Start メソッド ()」の関連用語

Thread.Start メソッド ()のお隣キーワード
検索ランキング

   

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



Thread.Start メソッド ()のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS