public static LuaValue Wrap(LuaValue[] args) { LuaFunction f = args[0] as LuaFunction; LuaCoroutine c = new LuaCoroutine(f); LuaFunction f2 = new LuaFunction(new LuaFunc(delegate(LuaValue[] args2) { return LuaBoolean.From(c.Resume(args2)); })); return f2; }
public static LuaValue Create(LuaValue[] args) { LuaFunction func = args[0] as LuaFunction; if (func == null) throw new ArgumentException("Function expected, got '" + args[0].Value.GetType().Name + "'"); LuaCoroutine c = new LuaCoroutine(func); return c; }
public bool Resume(LuaValue[] args) { if (thread == null) { thread = new Thread(new ThreadStart(delegate() { try { _status = "running"; func.Invoke(args); _status = "dead"; Running = null; } catch (Exception) { _status = "dead"; } })); thread.SetApartmentState(ApartmentState.MTA); thread.Start(); } else { if (_status == "dead") throw new Exception("Error: coroutine is dead, it cannot be resumed!"); try { if (_status == "suspended") thread.Resume(); else thread.Start(); _status = "running"; } catch (Exception ex) { _status = "dead"; throw ex; } } Running = this; return true; }
public bool Resume(LuaValue[] args) { if (thread == null) { thread = new Thread(new ThreadStart(delegate() { try { _status = "running"; func.Invoke(args); _status = "dead"; Running = null; } catch (Exception e) { A8Console.WriteLine("Coroutine exception: " + e.GetType().Name + " - " + e.Message + "\n" + e.StackTrace); _status = "dead"; } })); //thread.SetApartmentState(ApartmentState.MTA); thread.Start(); } else { if (_status == "dead") throw new Exception("Error: coroutine is dead, it cannot be resumed!"); try { if (_status == "suspended") lock (thread) { Monitor.Pulse(thread); } else { thread.Start(); } _status = "running"; } catch (Exception ex) { _status = "dead"; throw ex; } } Running = this; return true; }