How to sync this asynch process ?

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

Hello,

I'm inserting a new row in a Access database containing an
"AutoIncrement" column.
The autoincrement value is set by the database once the update occurs.
The only way to get this value is through an event delegate.

My question is inside the comments :

....
....
....

// Call to a function raising a delegated event which attributes
// a value to the lProductId variable
ret = MyFuncReturningResultThroughDelegate()

// How to stop here until the lProductId is newly affected
// as the previous call returns immediatly even if the delegate
// is not yet reached.
// (I want to wait the delegate to make its job)
....
....
....

I would think about a binary semaphore synch mechanism (is it the good
mechanism to choose ?) but i'm not sure how (which API) should be used
in C# to implement it.

Any ideas ?


Regards,
Cybertof.
 
You've got it right, assuming the async call cannot be avoided. Just create
a System.Threading.ManualResetEvent before making the async call, and then
do WaitOne() on the ManualResetEvent. In the delegate, do Set() on the
ManualResetEvent and your waiting code will continue.

If you have any other non-CPU-bound work you might be doing, such as disk
I/O, setting up another DB call, etc., do it while you're waiting for the
callback before you call WaitOne(). That's otherwise wasted time.
 
Back
Top