} // func resume #endregion #region -- yield ------------------------------------------------------------------ /// <summary>Sends a result to the main-thread.</summary> /// <param name="args">Result</param> /// <returns>New parameter.</returns> public static LuaResult yield(object[] args) { // search for the correct thread LuaThread t = null; lock (luaThreads) t = luaThreads.Find(c => c.iCoroutineThreadId == Thread.CurrentThread.ManagedThreadId); if (t == null) { throw new LuaRuntimeException(Properties.Resources.rsCoroutineWrongThread, 2, false); } // yield value t.currentYield = args; t.evYield.Set(); // wait for next arguments t.evResume.Reset(); t.evResume.Wait(); // return the arguments or stop the background thread if (t.evResume == null) { Thread.CurrentThread.Abort(); } if (t.currentArguments != null) { return(new LuaResult(t.currentArguments)); } else { return(LuaResult.Empty); } } // func Yield
} // prop Status /// <summary>Returns the status of the thread.</summary> /// <param name="co">Thread</param> /// <returns>Returns the status.</returns> public static LuaResult status(object co) { LuaThread luaThread = co as LuaThread; if (luaThread != null) { switch (luaThread.Status) { case LuaThreadStatus.Dead: return(new LuaResult(csDead)); case LuaThreadStatus.Normal: return(new LuaResult(csNormal)); case LuaThreadStatus.Running: return(new LuaResult(csRunning)); case LuaThreadStatus.Suspended: return(new LuaResult(csSuspended)); default: throw new LuaRuntimeException("thead-status invalid", 2, false); } } else { throw new LuaRuntimeException(Properties.Resources.rsCoroutineInvalidCO, 2, false); } } // func running
} // prop Status /// <summary>Returns the status of the thread.</summary> /// <param name="co">Thread</param> /// <returns>Returns the status.</returns> public static LuaResult status(object co) { LuaThread luaThread = co as LuaThread; Thread clrThread = co as Thread; if (luaThread != null) { switch (luaThread.Status) { case LuaThreadStatus.Dead: return(new LuaResult(csDead)); case LuaThreadStatus.Normal: return(new LuaResult(csNormal)); case LuaThreadStatus.Running: return(new LuaResult(csRunning)); case LuaThreadStatus.Suspended: return(new LuaResult(csSuspended)); default: throw new LuaRuntimeException("thead-status invalid", 2, false); } } else if (clrThread != null) { ThreadState s = clrThread.ThreadState; if ((s & (ThreadState.Aborted | ThreadState.AbortRequested | ThreadState.Stopped | ThreadState.StopRequested)) != 0) { return(new LuaResult(csDead)); } else if ((s & (ThreadState.Suspended | ThreadState.SuspendRequested | ThreadState.WaitSleepJoin)) != 0) { return(new LuaResult(csSuspended)); } else if ((s & ThreadState.Unstarted) != 0) { return(new LuaResult(csNormal)); } else { return(new LuaResult(csRunning)); } } else { throw new LuaRuntimeException(Properties.Resources.rsCoroutineInvalidCO, 2, false); } } // func running
/// <summary>Returns the running coroutine and a boolean if it is the main.</summary> /// <returns></returns> public static LuaResult running() { LuaThread t = null; lock (luaThreads) t = luaThreads.Find(c => c.iCoroutineThreadId == Thread.CurrentThread.ManagedThreadId); if (t == null) { return(new LuaResult(Thread.CurrentThread, true)); } else { return(new LuaResult(t, false)); } } // func running
/// <summary>Returns the running coroutine and a boolean if it is the main.</summary> /// <returns></returns> public static LuaResult running() { LuaThread t = null; lock (luaThreads) { if (Task.CurrentId.HasValue) { t = luaThreads.Find(c => c.taskExecute != null && c.taskExecute.Id == Task.CurrentId.Value); } } return(t == null ? new LuaResult(null, true) : new LuaResult(t, false)); } // func running
/// <summary>Sends a result to the main-thread.</summary> /// <param name="args">Result</param> /// <returns>New parameter.</returns> public static LuaResult yield(object[] args) { // search for the correct thread LuaThread t = null; lock (luaThreads) { if (Task.CurrentId.HasValue) { t = luaThreads.Find(c => c.taskExecute != null && c.taskExecute.Id == Task.CurrentId.Value); } } if (t == null) { throw new LuaRuntimeException(Properties.Resources.rsCoroutineWrongThread, 2, false); } // yield value t.currentYield = args; t.evYield.Set(); // wait for next arguments t.evResume.Reset(); t.evResume.Wait(); // return the arguments or stop the background thread if (t.evResume == null) { t.taskCancelExecute.Cancel(); } if (t.currentArguments != null) { return(new LuaResult(t.currentArguments)); } else { return(LuaResult.Empty); } } // func Yield
} // func running /// <summary>Creates a function, that wraps resume.</summary> /// <param name="dlg"></param> /// <returns></returns> public static Func <object[], LuaResult> wrap(Delegate dlg) { LuaThread t = new LuaThread(dlg); return(new Func <object[], LuaResult>(args => t.resume(args))); } // func wrap
} // func Resume /// <summary>Executes a part of the thread.</summary> /// <param name="co">Thread, that should resume.</param> /// <param name="args">Arguments, that will pass to the function-part</param> /// <returns>Result of the function.</returns> public static LuaResult resume(LuaThread co, object[] args) { return(co.resume(args)); } // func resume
/// <summary>Executes a part of the thread.</summary> /// <param name="co">Thread, that should resume.</param> /// <param name="args">Arguments, that will pass to the function-part</param> /// <returns>Result of the function.</returns> public static LuaResult resume(LuaThread co, object[] args) => co.resume(args);
} // func running /// <summary>Creates a function, that wraps resume.</summary> /// <param name="dlg"></param> /// <returns></returns> public static Func<object[], LuaResult> wrap(Delegate dlg) { LuaThread t = new LuaThread(dlg); return new Func<object[], LuaResult>(args => t.resume(args)); } // func wrap
} // func Resume /// <summary>Executes a part of the thread.</summary> /// <param name="co">Thread, that should resume.</param> /// <param name="args">Arguments, that will pass to the function-part</param> /// <returns>Result of the function.</returns> public static LuaResult resume(LuaThread co, object[] args) { return co.resume(args); } // func resume