3

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?

atconway
  • 20,624
  • 30
  • 159
  • 229
  • See [C# passing extra parameters to an event handler?](http://stackoverflow.com/questions/4215845/c-sharp-passing-extra-parameters-to-an-event-handler), [C#: passing parameters to callbacks](http://stackoverflow.com/questions/3184947/c-passing-parameters-to-callbacks), and many others. – Raymond Chen Jun 04 '13 at 20:00
  • @RaymondChen - Actually I did and it was not identical at least in the implemented solution. Basically I could not infer from those posts the answer to my question. The answer from ChrisSinclair appears to be spot on once ironed out. Maybe it was just my lack of understanding though, so thanks for the links. – atconway Jun 04 '13 at 20:19
  • 1
    Identical in shape, just the names are different. You create a lambda that takes the event parameters and passes them to the callback with any additional parameters. In the first case, `id => AfterSaveMethod2(id, index)` is basically the same as `(state, timeout) => MyCallbackMethod(state, timeout, "ABC123")`, just with `id` changed to `state, timeout` and `index` changed to `"ABC123"`. Similarly, if you take `(sender) => evHandler(sender,someData)` and change `sender` to `state, timeout` and `someData` to `"ABC123"` then you have your answer. It's the same pattern. – Raymond Chen Jun 04 '13 at 20:44
  • @RaymondChen - Thanks for connecting the dots. Since the usage was new to me, I needed it applied to my specific example to understand better. – atconway Jun 04 '13 at 21:05

1 Answers1

3

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);
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • I like the 1st solution, but when I try it I get a message indicating `Cannot resolve method MyCallBackMethod...` indicating there is no overload that only takes a single parameter. The 2nd suggestion works at least at build. Does the response message for the 1st solution mean I did it wrong? – atconway Jun 04 '13 at 20:16
  • Chris provided two versions depending on whether you want `MyCallbackMethod` to have 1 or 3 parameters. Pick the one that works for you. – Raymond Chen Jun 04 '13 at 20:45
  • @RaymondChen - Using `RegisterWaitForSingleObject` I *believe` requires the `state` and `timeout` parameters so I don't believe the 1st option will work if that was the purpose of showing the multiple ways. – atconway Jun 04 '13 at 21:07
  • @atconway I guess you have it figured out now. The lambda methods I provided here (both of them) _do_ manage the inputs for `state` and `timeout`. Only the first way throws those two parameters away when calling into your `MyCallBackMethod` method. The second way passes those parameters through to your method. Since you never specified the signature of your `MyCallBackMethod`, I just took an educated guess. – Chris Sinclair Jun 04 '13 at 21:13