Skip to content

Commit 717c0ee

Browse files
author
Bjarte Skogøy
committed
Use Begin/End pattern to not break .Net 4 compatibility.
1 parent 7b65866 commit 717c0ee

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

CefSharp.BrowserSubprocess/CefRenderProcess.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,29 @@ public Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId,
132132

133133
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
134134
}
135+
136+
public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state)
137+
{
138+
var tcs = new TaskCompletionSource<JavascriptResponse>(state);
139+
var task = EvaluateScriptAsync(browserId, frameId, script, timeout);
140+
task.ContinueWith(t =>
141+
{
142+
if (t.IsFaulted)
143+
tcs.TrySetException(t.Exception.InnerExceptions);
144+
else if (t.IsCanceled)
145+
tcs.TrySetCanceled();
146+
else
147+
tcs.TrySetResult(t.Result);
148+
149+
if (callback != null)
150+
callback(tcs.Task);
151+
});
152+
return tcs.Task;
153+
}
154+
155+
public JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result)
156+
{
157+
return ((Task<JavascriptResponse>)result).Result;
158+
}
135159
}
136160
}

CefSharp/Internals/BrowserProcessServiceHost.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ public void SetOperationContext(OperationContext operationContext)
5151
public Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout)
5252
{
5353
var operationContextTask = operationContextTaskCompletionSource.Task;
54-
5554
return operationContextTask.ContinueWith(t =>
5655
{
5756
var context = t.Result;
5857
var renderProcess = context.GetCallbackChannel<IRenderProcess>();
59-
return renderProcess.EvaluateScriptAsync(browserId, frameId, script, timeout);
58+
return Task.Factory.FromAsync<JavascriptResponse>(renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null), renderProcess.EndEvaluateScriptAsync);
6059
}).Unwrap();
6160
}
6261

CefSharp/Internals/IRenderProcess.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ namespace CefSharp.Internals
1111
[ServiceContract]
1212
public interface IRenderProcess
1313
{
14-
[OperationContract]
15-
Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout);
14+
[OperationContract(AsyncPattern=true)]
15+
IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state);
16+
17+
JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result);
18+
19+
//[OperationContract]
20+
//Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout);
1621
}
1722
}

0 commit comments

Comments
 (0)