0

I have a web service that instantiates an object, subscribes to an event on that object, and then calls a function on the object which will lead to the event being fired. Is there a way to wait for the event to fire so I can get the results from the EventArgs in order to pass those results as the results of the web service?

adam0101
  • 29,096
  • 21
  • 96
  • 174
  • 1
    Are the invocation of the method and the firing of the event synchronous or asynchronous? – Richard Blewett Jan 26 '12 at 17:05
  • That is to say calling the method on the object kicks off an asynchronous process that will eventually finish and fire the event. Is that what you meant? – adam0101 Jan 26 '12 at 18:26

1 Answers1

1

You will need some kind of synchronization primitive associated with the event (such as a ManualResetEventSlim) that gets signalled in the event handler. Then your request thread can wait on the event and collect the results after the event has occurred and generate the response

However, a more natural model may be to create the service as an asynchronous service and only complete the processing when the async operation completes - I created a sample of this a while back

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23
  • That sounds similar to what I found here: http://stackoverflow.com/questions/475617/wrapping-an-asynchronous-method-synchronously-in-c-sharp but that uses AutoResetEvent. What's the difference between AutoResetEvent, ManualResetEvent, and ManualResetEventSlim? – adam0101 Jan 26 '12 at 19:41
  • AutoResetEvent and ManualResetEvent are wrappers over the underlying kernel object whereas ManualResetEventSlim is a fully managed event (so a lot more lightweight). ManualResetEventSlim is only available in .NET 4.0 however. AutoResetEvents reset themselves to non-signalled after the waiter wakes up. Manual events must be manually reset – Richard Blewett Jan 26 '12 at 19:54