private async Task <PPError> OpenAsyncCore(URLRequestInfo requestInfo, MessageLoop openLoop = null) { var tcs = new TaskCompletionSource <PPError>(); EventHandler <PPError> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleOpen += handler; if (MessageLoop == null && openLoop == null) { Open(requestInfo); } else { Action <PPError> action = new Action <PPError>((e) => { var result = (PPError)PPBURLLoader.Open(this, requestInfo, new BlockUntilComplete() ); tcs.TrySetResult(result); } ); InvokeHelper(action, openLoop); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(PPError.Aborted); } finally { HandleOpen -= handler; } }
/// <summary> /// This function begins loading the <code>URLRequestInfo</code> asynchronously. /// The operation completes when response headers are received or when an /// error occurs. Use ResponseInfo to access the response /// headers. /// </summary> /// <param name="requestInfo">A <code>URLRequestInfo</code> corresponding to a /// URLRequestInfo.</param> /// <param name="openLoop">Optional MessageLoop instance that can be used to post the command to</param> /// <returns>Error code</returns> public Task <PPError> OpenAsync(URLRequestInfo requestInfo, MessageLoop openLoop = null) => OpenAsyncCore(requestInfo, openLoop);
/// <summary> /// This function begins loading the <code>URLRequestInfo</code>. /// The operation completes when response headers are received or when an /// error occurs. Use ResponseInfo to access the response /// headers. /// </summary> /// <param name="requestInfo">A <code>URLRequestInfo</code> corresponding to a /// URLRequestInfo.</param> /// <returns>Error code</returns> public PPError Open(URLRequestInfo requestInfo) => (PPError)PPBURLLoader.Open(this, requestInfo, new CompletionCallback(OnOpen));