public static bool Suspend(WaitHandle handle, int milliseconds) { RuntimeExecution current = EnsureInstance(); current.AwakeParentThread(); return(handle.WaitOne(milliseconds)); }
public static void SendUnhandledException(Exception ex) { Guard.ArgumentNotNull(ex, "ex"); RuntimeExecution current = EnsureInstance(); current.UnhandledException?.Invoke(null, new RuntimeUnhandledExceptionEventArgs(ex, EcmaValueUtility.GetValueFromException(ex))); }
private RuntimeExecution() { current = this; defaultRealm = new RuntimeRealm(); this.Thread = Thread.CurrentThread; this.CanSuspend = true; this.AutoExit = true; }
private static RuntimeExecution EnsureInstance() { if (current == null) { current = new RuntimeExecution(); } return(current); }
public static bool Continue() { RuntimeExecution current = EnsureInstance(); bool executed = false; DateTime? waitUntil = null; List <Job> actions = new List <Job>(current.queue); current.queue.Clear(); foreach (Job job in actions) { if (job.Realm.Disposed) { continue; } if (!waitUntil.HasValue || waitUntil > job.TriggerTime) { waitUntil = job.TriggerTime; } if (DateTime.Now < job.TriggerTime) { current.queue.Add(job); continue; } executed = true; if (job.RecurringMilliseconds > 0) { current.queue.Add(new Job { Id = job.Id, Realm = job.Realm, Action = job.Action, TriggerTime = DateTime.Now.AddMilliseconds(job.RecurringMilliseconds), RecurringMilliseconds = job.RecurringMilliseconds }); } try { job.Realm.Execute(job.Action); } catch (Exception ex) { SendUnhandledException(ex); } } if (!executed && !current.ShouldExit) { if (waitUntil >= DateTime.Now) { SuspendThread(waitUntil.Value - DateTime.Now); } else if (waitUntil == null) { SuspendThread(null); } } return(!current.ShouldExit); }
public static void SendUnhandledException(EcmaValue value) { Exception ex = null; if (value.GetUnderlyingObject() is EcmaError e) { ex = e.Exception; } RuntimeExecution current = EnsureInstance(); current.UnhandledException?.Invoke(null, new RuntimeUnhandledExceptionEventArgs(ex ?? EcmaException.FromValue(value), value)); }
public static bool Cancel(RuntimeExecutionHandle handle) { RuntimeExecution current = EnsureInstance(); if (handle.Id <= 0) { throw new ArgumentOutOfRangeException("handle", "Invalid handle"); } int index = current.queue.FindIndex(v => v.Id == handle.Id); if (index >= 0) { current.queue.RemoveAt(index); return(true); } return(false); }
public static RuntimeExecution CreateWorkerThread(RuntimeExecutionStart action, bool autoExit) { ManualResetEvent waitHandle = new ManualResetEvent(false); RuntimeRealm parentRealm = RuntimeRealm.Current; RuntimeExecution execution = null; Thread thread = new Thread(() => { execution = EnsureInstance(); execution.parentWaitHandle = waitHandle; execution.AutoExit = autoExit; try { action(parentRealm); execution.AwakeParentThread(); ContinueUntilEnd(); } catch (Exception ex) { SendUnhandledException(ex); execution.AwakeParentThread(); } }); thread.IsBackground = true; thread.Start(); waitHandle.WaitOne(); return(execution); }
public static RuntimeExecutionHandle Enqueue(Action action, int milliseconds, RuntimeExecutionFlags flags) { RuntimeExecution current = EnsureInstance(); return(current.Enqueue(RuntimeRealm.Current, action, milliseconds, flags)); }
public static void Enqueue(Action action, Task task) { RuntimeExecution current = EnsureInstance(); current.Enqueue(RuntimeRealm.Current, action, task); }