none
线程池怎么传递参数? RRS feed

  • 问题

  • 在msdn上找到了一个线程池的示例:
    Sub DoWork()
        ' Queue a task
        System.Threading.ThreadPool.QueueUserWorkItem( _
            New System.Threading.WaitCallback(AddressOf SomeLongTask))
        ' Queue another task
        System.Threading.ThreadPool.QueueUserWorkItem( _
            New System.Threading.WaitCallback(AddressOf AnotherLongTask))
    End Sub
    Sub SomeLongTask(ByVal state As Object)
        ' Insert code to perform a long task.
    End Sub
    Sub AnotherLongTask(ByVal state As Object)
        ' Insert code to perform another long task.
    End Sub
    如果两个过程都需要在运行的时候得到参数,在线程池里应该怎么调用呢?
    2009年8月24日 8:15

答案

  • 您好,QueueUserWorkItem 有个重载的方法
    Public Shared Function QueueUserWorkItem ( _
    	callBack As WaitCallback, _
    	state As Object _
    ) As Boolean

    通过state来传,具体请参考:
    ' 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

    • 已标记为答案 picat 2009年8月24日 14:46
    2009年8月24日 11:43

全部回复

  • 您好,QueueUserWorkItem 有个重载的方法
    Public Shared Function QueueUserWorkItem ( _
    	callBack As WaitCallback, _
    	state As Object _
    ) As Boolean

    通过state来传,具体请参考:
    ' 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

    • 已标记为答案 picat 2009年8月24日 14:46
    2009年8月24日 11:43
  •   谢谢楼上,我也看到这个重载方法并且搞明白了,之前有些不明白State怎么传值,呵呵.
    2009年8月24日 14:46