internal void InvokeHelper(Action <PPError> action, MessageLoop messageLoop = null, PPError result = PPError.OkCompletionpending) { if (messageLoop == null) { MessageLoop.PostWork(action); } else { if (messageLoop is BlockingMessageLoop) { action(result); } else { messageLoop.PostWork(action); } } }
private static async Task <ProxyInfo> GetProxyForURLAsyncCore(Instance instance, string url, MessageLoop messageLoop = null) { var tcs = new TaskCompletionSource <ProxyInfo>(); EventHandler <ProxyInfo> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleProxyForUrl += handler; if (messageLoop == null) { GetProxyForURL(instance, url); } else { Action <PPError> action = new Action <PPError>((e) => { var output = new PPVar(); var presult = (PPError)PPBNetworkProxy.GetProxyForURL(instance, new Var(url), out output, new BlockUntilComplete()); tcs.TrySetResult(new ProxyInfo(presult, ((Var)output).AsString())); } ); messageLoop.PostWork(action); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(new ProxyInfo(PPError.Aborted, string.Empty)); } finally { HandleProxyForUrl -= handler; } }