I have a console application and i use a threadpool, but my application exits before the threadpool execute its job. I read that i can use Console.ReadLine() but I want to exit immediately after threadpool done its job. I found examples in c# but i want something about vb.net, also i use .net 3.5 so i can’t use task parallel library. My current code is:
For i As Integer = 0 To addresses.Length - 1
Dim mydata_obj = New With {Key .address = addresses(i), .subject = messages.subject, .body = messages.body}
ThreadPool.QueueUserWorkItem(AddressOf doSomething, mydata_obj)
Next
use
WaitHandle.WaitAllto wait it until task completefor more information WaitHandle.WaitAll
Alternative is using
threadpoolWaiting for an object can also be achieved using the threadpool. In some situations this is better because it means you don’t need a dedicated thread to wait for the object.
ThreadPool class Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.