diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index edaf8ae7af..e32bf45958 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -132,5 +132,37 @@ public Task EvaluateScriptAsync(int browserId, long frameId, return timeout.HasValue ? task.WithTimeout(timeout.Value) : task; } + + public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state) + { + var tcs = new TaskCompletionSource(state); + var task = EvaluateScriptAsync(browserId, frameId, script, timeout); + task.ContinueWith(t => + { + if (t.IsFaulted) + { + tcs.TrySetException(t.Exception.InnerExceptions); + } + else if (t.IsCanceled) + { + tcs.TrySetCanceled(); + } + else + { + tcs.TrySetResult(t.Result); + } + + if (callback != null) + { + callback(tcs.Task); + } + }); + return tcs.Task; + } + + public JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result) + { + return ((Task)result).Result; + } } } diff --git a/CefSharp/Internals/BrowserProcessServiceHost.cs b/CefSharp/Internals/BrowserProcessServiceHost.cs index efbd413c63..930c63715d 100644 --- a/CefSharp/Internals/BrowserProcessServiceHost.cs +++ b/CefSharp/Internals/BrowserProcessServiceHost.cs @@ -51,12 +51,11 @@ public void SetOperationContext(OperationContext operationContext) public Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout) { var operationContextTask = operationContextTaskCompletionSource.Task; - return operationContextTask.ContinueWith(t => { var context = t.Result; var renderProcess = context.GetCallbackChannel(); - return renderProcess.EvaluateScriptAsync(browserId, frameId, script, timeout); + return Task.Factory.FromAsync(renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null), renderProcess.EndEvaluateScriptAsync); }).Unwrap(); } diff --git a/CefSharp/Internals/IRenderProcess.cs b/CefSharp/Internals/IRenderProcess.cs index b53303d72b..6e5645bcc3 100644 --- a/CefSharp/Internals/IRenderProcess.cs +++ b/CefSharp/Internals/IRenderProcess.cs @@ -11,7 +11,9 @@ namespace CefSharp.Internals [ServiceContract] public interface IRenderProcess { - [OperationContract] - Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout); + [OperationContract(AsyncPattern=true)] + IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state); + + JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result); } }