0

Hi im trying to create an implementation of scheduling using the EventWaitHandle class

Take the following example:

// Program 1
static void Main(string[] args)
{
    EventWaitHandle wh = new EventWaitHandle(false,EventResetMode.ManualReset,"MyCrossProcessEventHandle");
    wh.Set();
}

// Program 2
static void Main(string[] args)
{
    EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.ManualReset, "MyCrossProcessEventHandle");

    while (true)
    {
        var span = CalculateSpan();

        wh.WaitOne(span);

        // TODO Implement check, why did execution proceed? 
        // timeout ocurred
        // OR
        // Eventhandle was set
        //
        // if timeout occured
        // break the current iteration loop (And thereby calculate a new timeout)
        //
        // if event.set
        // continue execution




        // SNIP SNIP - More code here
    }
}

private static int CalculateSpan()
{
    DateTime datetoRun = new DateTime(2020,01,01,00,00,00);

    var span = datetoRun - DateTime.Now;

    if (span.TotalMilliseconds >= int.MaxValue)
    {
        return int.MaxValue;
    }

    return (int)span.TotalMilliseconds;
}

READ THE TODO IN THE CODE

Boiled down: So i want to be able to schedule an execution for more than int.MaxValue, and manually force execution cross process

Perhaps an easier way of achieving the exact same scenario?

Lars Nielsen
  • 436
  • 3
  • 16

2 Answers2

1

If all you're after is waiting for a set period of time before doing something you could use:

  • Thread.Sleep
    • Not recommended though as this blocks the thread and is generally considered bad practice (unless you're on a separate thread you can block)
  • Timer
    • The most common solution as it supports firing a timer event asynchronously so it doesn't interfere with your main thread etc.

You could also just spin up a new thread / Task and wait for a set period before doing something:

Task.Factory.StartNew(() =>
{
   Thread.Sleep(CalculateSpan());
   // Do something here because the time to wait has passed now
});
Clint
  • 6,133
  • 2
  • 27
  • 48
  • I thought it was clear given the code that i wanted to be able to force execution - cross process. Your answer does not grant that posibility - I've updated the question to reflect this. – Lars Nielsen Apr 07 '13 at 14:37
  • @LarsNielsen Ah, fair enough, no, it didn't make it particularly clear that that's what you were trying to achieve; you can do so either using some IPC with pipes and the such, or a `Mutex`, mutexes allow for the set / wait mechanism but cross-process. – Clint Apr 07 '13 at 14:42
  • Thanks for your answer allthough it doesn't answer the question. So perhaps my question is still not clear enough. I'll reedit – Lars Nielsen Apr 07 '13 at 14:51
1

After reading the documentation (DOH) i found the solution

    // Summary:
    //     Blocks the current thread until the current System.Threading.WaitHandle receives
    //     a signal, using a 32-bit signed integer to specify the time interval.
    //
    // SNIP SNIP
    //
    // Returns:
    //     true if the current instance receives a signal; otherwise, false.
    //
    // SNIP SNIP
    public virtual bool WaitOne(int millisecondsTimeout);
Lars Nielsen
  • 436
  • 3
  • 16