From 717c0eedbfd895a8755ca66872613c07159db47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjarte=20Skog=C3=B8y?= Date: Wed, 24 Sep 2014 14:13:09 +0200 Subject: [PATCH 1/2] Use Begin/End pattern to not break .Net 4 compatibility. --- .../CefRenderProcess.cs | 24 +++++++++++++++++++ .../Internals/BrowserProcessServiceHost.cs | 3 +-- CefSharp/Internals/IRenderProcess.cs | 9 +++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index edaf8ae7af..b120377f7e 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -132,5 +132,29 @@ 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..7dd54ad1fc 100644 --- a/CefSharp/Internals/IRenderProcess.cs +++ b/CefSharp/Internals/IRenderProcess.cs @@ -11,7 +11,12 @@ 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); + + //[OperationContract] + //Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout); } } From 17b56e694ab7cbe02f54292002fe865dec1b0746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjarte=20Skog=C3=B8y?= Date: Thu, 25 Sep 2014 13:49:39 +0200 Subject: [PATCH 2/2] brackets and removing commented code --- CefSharp.BrowserSubprocess/CefRenderProcess.cs | 8 ++++++++ CefSharp/Internals/IRenderProcess.cs | 3 --- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index b120377f7e..e32bf45958 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -140,14 +140,22 @@ public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string 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; } diff --git a/CefSharp/Internals/IRenderProcess.cs b/CefSharp/Internals/IRenderProcess.cs index 7dd54ad1fc..6e5645bcc3 100644 --- a/CefSharp/Internals/IRenderProcess.cs +++ b/CefSharp/Internals/IRenderProcess.cs @@ -15,8 +15,5 @@ public interface IRenderProcess IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state); JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result); - - //[OperationContract] - //Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout); } }