ThreadPool.QueueUserWorkItemとは? わかりやすく解説

ThreadPool.QueueUserWorkItem メソッド (WaitCallback, Object)

実行するためのキューメソッドを置き、そのメソッド使用するデータ含んだオブジェクト指定しますメソッドは、スレッド プールスレッド使用可能になったときに実行されます。

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

Public Shared Function QueueUserWorkItem
 ( _
    callBack As WaitCallback, _
    state As Object _
) As Boolean
Dim callBack As WaitCallback
Dim state As Object
Dim returnValue As Boolean

returnValue = ThreadPool.QueueUserWorkItem(callBack, state)
public static bool QueueUserWorkItem
 (
    WaitCallback callBack,
    Object state
)
public:
static bool QueueUserWorkItem (
    WaitCallback^ callBack, 
    Object^ state
)
public static boolean QueueUserWorkItem (
    WaitCallback callBack, 
    Object state
)
public static function QueueUserWorkItem
 (
    callBack : WaitCallback, 
    state : Object
) : boolean

パラメータ

callBack

実行するメソッドを表す WaitCallback。

state

メソッド使用するデータ格納したオブジェクト

戻り値
このメソッド正常にキュー置かれ場合trueそれ以外場合false

例外例外
解説解説
使用例使用例
Imports System
Imports System.Threading

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Queue the task.
        ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc)
 _
            )
        ' Note that you do not have to create the WaitCallback delegate
        ' explicitly in Visual Basic.  The following line also queues
 
        ' the task:
        'ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
        
        Console.WriteLine("Main thread does some work, then sleeps.")
        ' If you comment out the Sleep, the main thread exits before
        ' the thread pool task runs.  The thread pool uses background
        ' threads, which do not keep the application running.  (This
        ' is a simple example of a race condition.)
        Thread.Sleep(1000)

        Console.WriteLine("Main thread exits.")
    End Sub

    ' This thread procedure performs the task.
    Shared Sub ThreadProc(stateInfo As
 Object)
        ' No state object was passed to QueueUserWorkItem, so 
        ' stateInfo is null.
        Console.WriteLine("Hello from the thread pool.")
    End Sub
End Class
using System;
using System.Threading;
public class Example {
    public static void Main()
 {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        
        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
 {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}
using namespace System;
using namespace System::Threading;
ref class Example
{
public:

   // This thread procedure performs the task.
   static void ThreadProc( Object^ stateInfo
 )
   {
      
      // No state object was passed to QueueUserWorkItem, so 
      // stateInfo is 0.
      Console::WriteLine( "Hello from the thread pool." );
   }

};

int main()
{
   
   // Queue the task.
   ThreadPool::QueueUserWorkItem( gcnew WaitCallback( Example::ThreadProc ) );
   Console::WriteLine( "Main thread does some work, then sleeps." );
   
   // If you comment out the Sleep, the main thread exits before
   // the thread pool task runs.  The thread pool uses background
   // threads, which do not keep the application running.  (This
   // is a simple example of a race condition.)
   Thread::Sleep( 1000 );
   Console::WriteLine( "Main thread exits." );
   return 0;
}

import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Example
{
    public static void main(String[]
 args)
    {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        Console.WriteLine("Main thread does some work, then sleeps.");

        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);
        Console.WriteLine("Main thread exits.");
    } //main

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    } //ThreadProc
} //Example
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ThreadPool.QueueUserWorkItem メソッド (WaitCallback)

メソッド実行するためのキュー置きますメソッドは、スレッド プールスレッド使用可能になったときに実行されます。

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

Public Shared Function QueueUserWorkItem
 ( _
    callBack As WaitCallback _
) As Boolean
Dim callBack As WaitCallback
Dim returnValue As Boolean

returnValue = ThreadPool.QueueUserWorkItem(callBack)
public static bool QueueUserWorkItem
 (
    WaitCallback callBack
)
public:
static bool QueueUserWorkItem (
    WaitCallback^ callBack
)
public static boolean QueueUserWorkItem (
    WaitCallback callBack
)
public static function QueueUserWorkItem
 (
    callBack : WaitCallback
) : boolean

パラメータ

callBack

実行するメソッドを表す WaitCallback

戻り値
このメソッド正常にキュー置かれ場合trueそれ以外場合false

例外例外
解説解説
使用例使用例
' This example shows how to create an object containing task
' information, and pass that object to a task queued for
' execution by the thread pool.
Imports System
Imports System.Threading
' TaskInfo holds state information for a task that will be
' executed by a ThreadPool thread.
Public Class TaskInfo
    ' State information for the task.  These members
    ' can be implemented as read-only properties, read/write
    ' properties with validation, and so on, as required.
    Public Boilerplate As String
    Public Value As Integer

    ' Public constructor provides an easy way to supply all
    ' the information needed for the task.
    Public Sub New(text
 As String, number As Integer)
        Boilerplate = text
        Value = number
    End Sub
End Class

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Create an object containing the information needed
        ' for the task.
        Dim ti As New TaskInfo("This
 report displays the number {0}.", 42)

        ' Queue the task and data.
        If ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc),
 ti) Then
        
            Console.WriteLine("Main thread does some work, then
 sleeps.")

            ' If you comment out the Sleep, the main thread exits before
            ' the ThreadPool task has a chance to run.  ThreadPool uses
 
            ' background threads, which do not keep the application
 
            ' running.  (This is a simple example of a race condition.)
            Thread.Sleep(1000)

            Console.WriteLine("Main thread exits.")
        Else
            Console.WriteLine("Unable to queue ThreadPool request.")
        End If
    End Sub

    ' The thread procedure performs the independent task, in this case
    ' formatting and printing a very simple report.
    '
    Shared Sub ThreadProc(stateInfo As
 Object)
        Dim ti As TaskInfo = CType(stateInfo,
 TaskInfo)
        Console.WriteLine(ti.Boilerplate, ti.Value)
    End Sub
End Class
// This example shows how to create an object containing task
// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo {
    // State information for the task.  These members
    // can be implemented as read-only properties, read/write
    // properties with validation, and so on, as required.
    public string Boilerplate;
    public int Value;

    // Public constructor provides an easy way to supply all
    // the information needed for the task.
    public TaskInfo(string text, int
 number) {
        Boilerplate = text;
        Value = number;
    }
}

public class Example {
    public static void Main()
 {
        // Create an object containing the information needed
        // for the task.
        TaskInfo ti = new TaskInfo("This report displays
 the number {0}.", 42);

        // Queue the task and data.
        if (ThreadPool.QueueUserWorkItem(new
 WaitCallback(ThreadProc), ti)) {    
            Console.WriteLine("Main thread does some work, then sleeps.");

            // If you comment out the Sleep, the main thread exits before
            // the ThreadPool task has a chance to run.  ThreadPool
 uses 
            // background threads, which do not keep the application
 
            // running.  (This is a simple example of a race condition.)
            Thread.Sleep(1000);

            Console.WriteLine("Main thread exits.");
        }
        else {
            Console.WriteLine("Unable to queue ThreadPool request."); 
        }
    }

    // The thread procedure performs the independent task, in this case
    // formatting and printing a very simple report.
    //
    static void ThreadProc(Object stateInfo)
 {
        TaskInfo ti = (TaskInfo) stateInfo;
        Console.WriteLine(ti.Boilerplate, ti.Value); 
    }
}
// This example shows how to create an Object* containing task
// information, and pass that Object* to a task queued for
// execution by the thread pool.
using namespace System;
using namespace System::Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public ref class TaskInfo
{
public:

   // State information for the task.  These members
   // can be implemented as read-only properties, read/write
   // properties with validation, and so on, as required.
   String^ Boilerplate;
   int Value;

   // Public constructor provides an easy way to supply all
   // the information needed for the task.
   TaskInfo( String^ text, int number )
   {
      Boilerplate = text;
      Value = number;
   }

};

public ref struct Example
{
public:

   // The thread procedure performs the independent task, in this case
   // formatting and printing a very simple report.
   //
   static void ThreadProc( Object^ stateInfo
 )
   {
      TaskInfo^ ti = dynamic_cast<TaskInfo^>(stateInfo);
      Console::WriteLine( ti->Boilerplate, ti->Value );
   }

};

int main()
{
   
   // Create an object containing the information needed
   // for the task.
   TaskInfo^ ti = gcnew TaskInfo( "This report displays the number {0}.",42
 );
   
   // Queue the task and data.
   if ( ThreadPool::QueueUserWorkItem( gcnew WaitCallback( Example::ThreadProc
 ), ti ) )
   {
      Console::WriteLine( "Main thread does some work, then sleeps." );
      
      // If you comment out the Sleep, the main thread exits before
      // the ThreadPool task has a chance to run.  ThreadPool uses 
      // background threads, which do not keep the application 
      // running.  (This is a simple example of a race condition.)
      Thread::Sleep( 1000 );
      Console::WriteLine( "Main thread exits." );
   }
   else
   {
      Console::WriteLine( "Unable to queue ThreadPool request." );
   }

   return 0;
}

// This example shows how to create an object containing task
// information, and pass that object to a task queued for
// execution by the thread pool.
import System.*;
import System.Threading.*;
import System.Threading.Thread;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo
{
    // State information for the task.  These members
    // can be implemented as read-only properties, read/write
    // properties with validation, and so on, as required.
    public String boilerplate;
    public int value;

    // Public constructor provides an easy way to supply all
    // the information needed for the task.
    public TaskInfo(String text, int number)
    {
        boilerplate = text;
        value = number;
    } //TaskInfo
} //TaskInfo

public class Example
{
    public static void main(String[]
 args)
    {
        // Create an object containing the information needed
        // for the task.
        TaskInfo ti = new TaskInfo("This report displays
 the number {0}.", 42);

        // Queue the task and data.
        if (ThreadPool.QueueUserWorkItem(new
 WaitCallback(ThreadProc), ti)) {
            Console.WriteLine("Main thread does some work, then sleeps.");

            // If you comment out the Sleep, the main thread exits before
            // the ThreadPool task has a chance to run.  ThreadPool
 uses 
            // background threads, which do not keep the application
 
            // running.  (This is a simple example of a race condition.)
            Thread.Sleep(1000);
            Console.WriteLine("Main thread exits.");
        }
        else {
            Console.WriteLine("Unable to queue ThreadPool request.");
        }
    } //main

    // The thread procedure performs the independent task, in this case
    // formatting and printing a very simple report.
    //
    static void ThreadProc(Object stateInfo)
    {
        TaskInfo ti = ((TaskInfo)(stateInfo));

        Console.WriteLine(ti.boilerplate, String.valueOf(ti.value));
    } //ThreadProc
} //Example
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ThreadPool.QueueUserWorkItem メソッド




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

辞書ショートカット

すべての辞書の索引

「ThreadPool.QueueUserWorkItem」の関連用語

ThreadPool.QueueUserWorkItemのお隣キーワード
検索ランキング

   

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



ThreadPool.QueueUserWorkItemのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS