namespace Microshaoft { using System; using System.Threading; public class WakeProcessor<TData> { private delegate void ProcessCaller(AutoResetEvent wait, TData data); public delegate void WaitEventHandler(WakeProcessor<TData> sender, TData data); public event WaitEventHandler OnWaitOneTimeout; public event WaitEventHandler OnWaitOneWake; public event WaitEventHandler OnWaitComplete; private int _waitOneTimeoutSeconds = 0; private int _waitTimeoutMinutes = 0; public WakeProcessor ( AutoResetEvent waitHandle , int waitOneTimeoutSeconds , int waitTimeoutMinutes , TData data ) { BeginWaiting ( waitHandle , waitOneTimeoutSeconds , waitTimeoutMinutes , data ); } public void BeginWaiting ( AutoResetEvent waitHandle , int waitOneTimeoutSeconds , int waitTimeoutMinutes , TData data ) { _waitOneTimeoutSeconds = waitOneTimeoutSeconds; _waitTimeoutMinutes = waitTimeoutMinutes; ProcessCaller handler = new ProcessCaller(Process); //AsyncCallback callback = new AsyncCallback(ProcessedCallback); //IAsyncResult result = handler.BeginInvoke(waitHandle, data , null, null); } private void Process(AutoResetEvent wait, TData data) { DateTime beginTime = DateTime.Now; while (DateTime.Now.Subtract(beginTime).TotalSeconds <= _waitTimeoutMinutes * 60) { bool r = wait.WaitOne(_waitOneTimeoutSeconds * 1000); if (r) { if (OnWaitOneWake != null) { OnWaitOneWake(this, data); } } else { if (OnWaitOneTimeout != null) { OnWaitOneTimeout(this, data); } } //if (r) //{ // break; //} } //if (!r) { if (OnWaitComplete != null) { OnWaitComplete(this, data); } } } private void ProcessedCallback(IAsyncResult result) { } } }