Choosing a thread control strategy

  • Thread starter Thread starter Jacob
  • Start date Start date
J

Jacob

Ok, I have a C# program (server program) which needs to do the following:
Receive messages from a webapp via WCF and notify the server that it has
received a new transaction. These transactions have a definite end date.
I've got the WCF setuff set up, and basically the server program just needs
to sort the transaction (think auction-ish) showing the next one to end.
Once it ends, the server program needs to do some work associated with the
transaction.

The problem I've run into is a conflict between how to handle the various
pieces. I want the server process to Sleep (I guess Threading.Sleep) until
whenever the next transaction is scheduled to end.

However, I also need the server to be available to calculate the time until
the next transaction ending whenever a new transaction comes in (in case its
endtime is sooner than anything else in the system). I was thinking an
AutoResetEvent for this, but if I am using that then it waits until the event
is set before releasing. So, if a transaction ends it won't be processed
until another one comes in and sets the event. However, using sleep, the
thread will be asleep and won't be available to catch the event for the new
transactions immediately as I understand it.

What I need is some sort of blend of Sleep and a ResetEvent, like a
ResetEvent with a timer on it that will wake either when the flag is set or
when a certain amount of time passes. Is there any such thing, or can
somebody suggest another way to implement this?

Thanks in advance
 
What I need is some sort of blend of Sleep and a ResetEvent, like a
ResetEvent with a timer on it that will wake either when the flag is set or
when a certain amount of time passes.  Is there any such thing, or can
somebody suggest another way to implement this?

All the main locking constructs have this functionality - just look at
the overloads; for AutoResetEvent/ManualResetEvent, this would be
"WaitOne(timeout, false)". The bool return tells you whether you got
signalled vs a timout. Of course, you'll need to calculate a suitable
amount of time to wait, or check some other condition on waking...

Marc
 
Back
Top