我想要做的是,创建一个执行某些操作的应用程序。最多应该有 10 个线程在运行。
我有以下代码,它工作正常。我需要向“Somework”程序发送一个参数。我怎样才能做到这一点?
Module Module1
Sub Main()
Dim Task As New Action(AddressOf SomeWork)
dim I as integer
for i=1 to 20
If RunningThread < 10 Then
Task.BeginInvoke(AddressOf Callback, Nothing)
Threading.Interlocked.Increment(RunningThread)
Else
SyncLock (Lock)
tasks.Enqueue(Task)
End SyncLock
End If
next
Console.ReadLine()
End Sub
Private tasks As New Queue(Of action)
Private RunningThread As Integer
Private Lock As New Object
Dim I As Integer = 0
Private Sub SomeWork()
I += 1
Console.WriteLine(I & " doing some work - begin :: " & Now.ToString)
Threading.Thread.Sleep(10000)
Console.WriteLine(I & " doing some work - end :: " & Now.ToString)
End Sub
Private Sub Callback(ByVal o As Object)
If tasks.Count > 0 Then
Dim Task As Action
SyncLock (Lock)
Task = tasks.Dequeue
End SyncLock
Task.BeginInvoke(AddressOf Callback, Nothing)
Else
Threading.Interlocked.Decrement(RunningThread)
End If
End Sub
End Module
请帮忙。
谢谢