Question

I'm using a RegisterWaitForSingleObject call in it's basic usage form to call a method upon the timeout value provided and all is working well. However there is a particular scerio I'm using this where the code to call RegisterWaitForSingleObject happens in an event handler and I need to pass the callback method some additional infrmation. Currently the callback method has the following required signature:

public void MyCallBackMethod(object state, bool timedOut)

So I can do this technically:

public void MyCallBackMethod(object state, bool timedOut, string SomeValue)

However now I don't know which values to send manually for state and timeout when trying to add this value at the time of calling RegisterWaitForSingleObject

ThreadPool.RegisterWaitForSingleObject(_stop, MyCallBackMethod(?,?, "ABC123"), null, 5000, true);

How can I properly pass additional values to my callback method registered using RegisterWaitForSingleObject?

Was it helpful?

Solution

Can you leverage lambda's/closure to ignore the other parameters and just call the method how you want it with the values you want?

ThreadPool.RegisterWaitForSingleObject(
    _stop, 
    (state, timeout) => MyCallBackMethod("ABC123"), 
    null, 
    5000, 
    true);

Or if your MyCallBackMethod does take the state and timeout event arguments:

ThreadPool.RegisterWaitForSingleObject(
    _stop, 
    (state, timeout) => MyCallBackMethod(state, timeout, "ABC123"), 
    null, 
    5000, 
    true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top