Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CefSharp.BrowserSubprocess/CefRenderProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,37 @@ public Task<JavascriptResponse> 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<JavascriptResponse>(state);
var task = EvaluateScriptAsync(browserId, frameId, script, timeout);
task.ContinueWith(t =>
{
if (t.IsFaulted)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if blocks need to have braces, keeps the style consistent.

{
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<JavascriptResponse>)result).Result;
}
}
}
3 changes: 1 addition & 2 deletions CefSharp/Internals/BrowserProcessServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ public void SetOperationContext(OperationContext operationContext)
public Task<JavascriptResponse> 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<IRenderProcess>();
return renderProcess.EvaluateScriptAsync(browserId, frameId, script, timeout);
return Task.Factory.FromAsync<JavascriptResponse>(renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null), renderProcess.EndEvaluateScriptAsync);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are AsyncCallback callback and object state required if we're only going to pass in null?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are needed yes. Omitting them gives a runtime exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, thx

}).Unwrap();
}

Expand Down
6 changes: 4 additions & 2 deletions CefSharp/Internals/IRenderProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace CefSharp.Internals
[ServiceContract]
public interface IRenderProcess
{
[OperationContract]
Task<JavascriptResponse> 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);
}
}