When using a thread pool and its queuecallbackitem, can I not pass in a func object (from the method parameter)?
I don’t see a func which takes one parameter but returns nothing. There isfunc<T, TResult> but how can I set TResult to be null (want to indicate ‘this method returns void’)?
Also, how could I use the threadpool for methods which return and take all sorts of paremeters? Could I not store Func objects in a generic collection and also an int to indicate priority, then execute those funcs?
Finally, in a static object (such as collection), what synchronisation in a global application would it need?
The only
Func<...>/Action<...>delegate that is similar toWaitCallbackisAction<object>. It won’t be directly usable; however, you can wrap delegates inside eachother:To return a result, one option is to update external state. Lambdas / anon-methods are good for this, since they offer closure support:
After execution,
result(from the above context) will be updated. However, a callback is more common:Where the callback function does something useful with the result.
Hopefully that also shows how you can execute arbitrary functions in a thread-pool thread.
Re synchronization; you are on a secondary thread, so you definitely would need synchronization if talking to any shared state. However, you might choose to use the UI to synchronize the result (if suitable) – i.e. (from a winform):