开发者

queing jobs in threadpool vb.net

i have 20,000 items in a queue, and i want to process them using the threadpool. will 开发者_JAVA百科this be the best way to do it?

for i as integer = 0 to 19999
ThreadPool.QueueUserWorkItem (PerformTask, ListTask(i))
next

Sub PerformTask(i as string)
' do the work here
end sub

How can i return or set ui control from the PerformTask sub?


You cannot.

However, you can allocate a container (array, list) with a different slot for each result, and write into it. Alternatively, you could pass an object into the worker method that holds both the input and the result. I’d use this method:

Class TaskObject
    Dim Input As String
    Dim Result As Whatever
End Class

Dim tasks As TaskObject() = New TaskObject(20000) { }

For i as Integer = 0 to tasks.Length - 1
    ThreadPool.QueueUserWorkItem(PerformTask, tasks(i))
next

Sub PerformTask(arg As Object)
    Dim task As TaskObject = DirectCast(arg, TaskObject)
    ' do the work here
end sub

Unrelated: you should always enable Option Strict in your projects. No exception. Your code has type errors that the compiler should detect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜