public bool DoOnePriorityEvent(out Async.PostEvent oe) { oe = null; Async.PostEvent e; lock (PriorityEvents) { if (PriorityEvents.Count == 0) { return(false); } e = PriorityEvents.Dequeue(); } try { if (e.PostAction != null) { var ret = e.PostAction(); var caw = e.Awaiter as Async.TaskAwaiter; if (caw != null) { caw.SetResult(ret); } } ExecuteContinue(e.Awaiter); } catch (Exception ex) { Profiler.Log.WriteException(ex); e.ExceptionInfo = ex; } oe = e; return(true); }
public async System.Threading.Tasks.Task AwaitAsyncIOEmpty() { var eh = new PostEvent(); eh.PostAction = null; eh.AsyncTarget = null; eh.ContinueThread = ContextThread.CurrentContext; eh.AsyncType = EAsyncType.AsyncIOEmpty; eh.Tag = null; var source = new System.Threading.Tasks.TaskCompletionSource <object>(); await source.Task.AwaitPost(eh); }
public async System.Threading.Tasks.Task <T> Post <T>(FPostEventReturn <T> evt, EAsyncTarget target = EAsyncTarget.AsyncIO) { var eh = new PostEvent(); ContextThread ctx = GetContext(target); eh.AsyncTarget = ctx; eh.ContinueThread = ContextThread.CurrentContext; if (eh.ContinueThread == this.ContextRHI) { switch (eh.ContinueThread.TickFlag) { case 1: eh.ContinueThread = this.ContextMain; break; case 2: eh.ContinueThread = this.ContextRHI; break; case 3: eh.ContinueThread = this.ContextEditor; break; } } FPostEvent ue = () => { try { var result = evt(); return(result); } catch (Exception ex) { Profiler.Log.WriteException(ex); return(default(T)); } }; eh.PostAction = ue; var source = new System.Threading.Tasks.TaskCompletionSource <object>(); object ret = await source.Task.AwaitPost(eh); try { return((T)ret); } catch { return(default(T)); } }
public async System.Threading.Tasks.Task DelayTime(int time) { var eh = new PostEvent(); eh.PostAction = null; eh.ContinueThread = ContextThread.CurrentContext; eh.AsyncType = EAsyncType.Delay; eh.PostAction = () => { System.Threading.Thread.Sleep(time); return(null); }; var source = new System.Threading.Tasks.TaskCompletionSource <object>(); await source.Task.AwaitPost(eh); }
public async System.Threading.Tasks.Task AwaitSemaphore(ASyncSemaphore smp) { if (smp == null) { Profiler.Log.WriteLine(Profiler.ELogTag.Error, "", $"AwaitSemaphore is null"); return; } var eh = new PostEvent(); eh.PostAction = null; eh.AsyncTarget = null; eh.ContinueThread = ContextThread.CurrentContext; eh.AsyncType = EAsyncType.Semaphore; eh.Tag = smp; smp.PostEvent = eh; var source = new System.Threading.Tasks.TaskCompletionSource <object>(); await source.Task.AwaitPost(eh); smp = null; }
//protected void DoPriorityEvents() //{ // while(PriorityEvents.Count>0) // { // Async.PostEvent e; // lock (PriorityEvents) // { // e = PriorityEvents.Dequeue(); // } // try // { // if (e.PostAction != null) // { // var ret = e.PostAction(); // var caw = e.Awaiter as Async.TaskAwaiter; // if(caw!=null) // caw.SetResult(ret); // } // ExecuteContinue(e.Awaiter); // } // catch (Exception ex) // { // Profiler.Log.WriteException(ex); // e.ExceptionInfo = ex; // } // } //} protected bool DoOneAsyncEvent(out Async.PostEvent oe) { oe = null; Async.PostEvent e; lock (AsyncEvents) { if (AsyncEvents.Count == 0) { return(false); } e = AsyncEvents.Dequeue(); } try { var ret = e.PostAction(); var caw = e.Awaiter as Async.TaskAwaiter; caw.SetResult(ret); } catch (Exception ex) { Profiler.Log.WriteException(ex); e.ExceptionInfo = ex; var caw = e.Awaiter as Async.TaskAwaiter; caw.SetResult(null); } if (e.Tag == null) { if (e.ContinueThread != null) { lock (e.ContinueThread.ContinueEvents) { e.ContinueThread.ContinueEvents.Enqueue(e); } } } oe = e; return(true); }
public override void Tick() { Async.ContextThreadManager.Instance.mTPoolTrigger.WaitOne(20); var events = Async.ContextThreadManager.Instance.TPoolEvents; while (true) { Async.PostEvent e = null; lock (events) { if (events.Count > 0) { e = events.Dequeue(); } else { break; } } if (e != null) { try { e.PostAction(); } catch (Exception ex) { Profiler.Log.WriteException(ex); e.ExceptionInfo = ex; } CEngine.Instance.EventPoster.mRunOnPEAllocator.ReleaseObject(e); } } Async.ContextThreadManager.Instance.mTPoolTrigger.Reset(); TickAwaitEvent(); }
protected bool DoOneContnueEvent(out Async.PostEvent oe) { oe = null; Async.PostEvent e; lock (ContinueEvents) { if (ContinueEvents.Count == 0) { return(false); } e = ContinueEvents.Dequeue(); } try { ExecuteContinue(e.Awaiter); } catch (Exception ex) { Profiler.Log.WriteException(ex); e.ExceptionInfo = ex; } oe = e; return(true); }
public static TaskAwaiter AwaitPost(this System.Threading.Tasks.Task task, PostEvent pe) { pe.Awaiter = new TaskAwaiter(task, pe); return(pe.Awaiter); }
public TaskAwaiter(System.Threading.Tasks.Task task, PostEvent pe) { PEvent = pe; pe.Awaiter = this; this.task = task; }