Ejemplo n.º 1
0
        public static bool Suspend(WaitHandle handle, int milliseconds)
        {
            RuntimeExecution current = EnsureInstance();

            current.AwakeParentThread();
            return(handle.WaitOne(milliseconds));
        }
Ejemplo n.º 2
0
        public static void SendUnhandledException(Exception ex)
        {
            Guard.ArgumentNotNull(ex, "ex");
            RuntimeExecution current = EnsureInstance();

            current.UnhandledException?.Invoke(null, new RuntimeUnhandledExceptionEventArgs(ex, EcmaValueUtility.GetValueFromException(ex)));
        }
Ejemplo n.º 3
0
 private RuntimeExecution()
 {
     current         = this;
     defaultRealm    = new RuntimeRealm();
     this.Thread     = Thread.CurrentThread;
     this.CanSuspend = true;
     this.AutoExit   = true;
 }
Ejemplo n.º 4
0
 private static RuntimeExecution EnsureInstance()
 {
     if (current == null)
     {
         current = new RuntimeExecution();
     }
     return(current);
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        public static RuntimeExecutionHandle Enqueue(Action action, int milliseconds, RuntimeExecutionFlags flags)
        {
            RuntimeExecution current = EnsureInstance();

            return(current.Enqueue(RuntimeRealm.Current, action, milliseconds, flags));
        }
Ejemplo n.º 10
0
        public static void Enqueue(Action action, Task task)
        {
            RuntimeExecution current = EnsureInstance();

            current.Enqueue(RuntimeRealm.Current, action, task);
        }