按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
.NET Framework
 QueueUserWorkItem 方法 (WaitCallback,...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework 類別庫
ThreadPool..::.QueueUserWorkItem 方法 (WaitCallback, Object)

佇列要執行的方法,並指定含方法所需資料的物件。可以使用執行緒集區執行緒時,即可執行這個方法。

命名空間:  System.Threading
組件:  mscorlib (在 mscorlib.dll)

Visual Basic (宣告)
Public Shared Function QueueUserWorkItem ( _
    callBack As WaitCallback, _
    state As Object _
) As Boolean
Visual Basic (使用方式)
Dim callBack As WaitCallback
Dim state As Object
Dim returnValue As Boolean

returnValue = ThreadPool.QueueUserWorkItem(callBack, _
    state)
C#
public static bool QueueUserWorkItem(
    WaitCallback callBack,
    Object state
)
Visual C++
public:
static bool QueueUserWorkItem(
    WaitCallback^ callBack, 
    Object^ state
)
J#
public static boolean QueueUserWorkItem(
    WaitCallback callBack,
    Object state
)
JScript
public static function QueueUserWorkItem(
    callBack : WaitCallback, 
    state : Object
) : boolean

參數

callBack
型別:System.Threading..::.WaitCallback

WaitCallback,表示要執行的方法。

state
型別:System..::.Object

物件,含方法所需的資料。

傳回值

型別:System..::.Boolean

如果方法成功佇列則為 true;如果工作項目無法佇列則會擲回 OutOfMemoryException

例外狀況條件
ApplicationException

發生了記憶體不足的狀況。

OutOfMemoryException

無法將此工作項目排入佇列中。

ArgumentNullException

callBacknullNothingnullptrNull 參照 (即 Visual Basic 中的 Nothing)

如果回呼方法需要複雜的資料,您可以定義包含這類資料的類別。

注意事項注意事項:

Visual Basic 使用者可以省略 WaitCallback 建構函式 (Constructor),而且只要在將回呼方法傳遞到 QueueUserWorkItem 時使用 AddressOf 運算子即可。Visual Basic 會自動呼叫正確的委派建構函式。

版本資訊

在 .NET Framework 2.0 版中,會使用 QueueUserWorkItem 方法將 Thread..::.CurrentPrincipal 屬性值傳送至排入佇列中的背景工作執行緒。在先前的版本中,並不會傳送主要資訊。

以下範例說明如何建立包含工作資訊的物件。此外還會示範,如何將該物件傳遞至佇列的工作,該工作將由執行緒集區執行。

Visual Basic
' 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

C#
// 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); 
    }
}

Visual C++
// 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;
}


J#
// 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

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

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱.NET Framework 系統需求

.NET Framework

支援版本:3.5、3.0 SP1、3.0、2.0 SP1、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0、1.0

XNA Framework

支援版本:1.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2008 Microsoft Corporation. All rights reserved. 使用規定  |  商標  |  隱私權聲明
Page view tracker