Wait for QueueUserWorkItem to Complete

You could use events to sync. Like this: private static ManualResetEvent resetEvent = new ManualResetEvent(false); public static void Main() { ThreadPool.QueueUserWorkItem(arg => DoWork()); resetEvent.WaitOne(); } public static void DoWork() { Thread.Sleep(5000); resetEvent.Set(); } If you don’t want to embed event set into your method, you could do something like this: var resetEvent = new ManualResetEvent(false); … Read more

Boost asio thread_pool join does not wait for tasks to be finished

The best practice is not to reuse the pool (what would be the use of pooling, if you keep creating new pools?). If you want to be sure you “time” the batches together, I’d suggest using when_all on futures: Live On Coliru #define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> #include <boost/thread.hpp> uint64_t foo(uint64_t begin) … Read more

How can I shutdown Spring task executor/scheduler pools before all other beans in the web app are destroyed?

Two ways: Have a bean implement ApplicationListener<ContextClosedEvent>. onApplicationEvent() will get called before the context and all the beans are destroyed. Have a bean implement Lifecycle or SmartLifecycle. stop() will get called before the context and all the beans are destroyed. Either way you can shut down the task stuff before the bean destroying mechanism takes … Read more

Can somebody explain this odd behavior when working with ThreadPool?

You’re closing over the loop variable, which gives you an unexpected result. Try this instead: foreach(string item in Items) { string item2 = item; Console.WriteLine(“Adding {0} to ThreadPool”, item2); ThreadPool.QueueUserWorkItem ( delegate { Load(item2, this.progCall, this.compCall); } ); numThreads++; Thread.Sleep(100);//Remove this line } References Closing over the Loop Variable in C# Closing over the loop … Read more