您好我试图使用EventWaitHandle类创建一个调度实现
采用以下示例:
// 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;
}
阅读代码中的TODO
简化:所以我希望能够安排执行超过int.MaxValue,并手动强制执行跨进程
也许是一种更简单的方法来实现完全相同的场景?
答案 0 :(得分:1)
如果您所做的一切都在等待一段时间后再做一些可以使用的事情:
Thread.Sleep
Timer
您还可以启动新线程/ Task
并等待一段时间后再做一些事情:
Task.Factory.StartNew(() =>
{
Thread.Sleep(CalculateSpan());
// Do something here because the time to wait has passed now
});
答案 1 :(得分:1)
阅读文档(DOH)后,我找到了解决方案
// 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);