Skip to content

Commit

Permalink
Add Experimental option where bound async methods are queued on TaskS…
Browse files Browse the repository at this point in the history
…cheduler.Default.

Set `CefSharpSettings.ConcurrentTaskExecution = true;` to enable this feature, should be set before the first ChromiumWebBrowser instance is created.
  • Loading branch information
amaitland committed May 4, 2017
1 parent 3015bc3 commit 767b478
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CefSharp.Example/CefExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ public static void Init(bool osr, bool multiThreadedMessageLoop, IBrowserProcess
}

Cef.AddCrossOriginWhitelistEntry(BaseUrl, "https", "cefsharp.com", false);

//Experimental option where bound async methods are queued on TaskScheduler.Default.
//CefSharpSettings.ConcurrentTaskExecution = true;
}

public static async void RegisterTestResources(IWebBrowser browser)
Expand Down
7 changes: 7 additions & 0 deletions CefSharp/CefSharpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ static CefSharpSettings()
/// NOTE: It isn't possble to change the proxy after the call to Cef.Initialize
/// </summary>
public static ProxyOptions Proxy { get; set; }

/// <summary>
/// This influences the behavior of RegisterAsyncJsObject and how method calls are made.
/// By default the <see cref="MethodRunnerQueue"/> executes Tasks in a sync fashion.
/// Setting this property to true will allocate new Tasks on TaskScheduler.Default for execution.
/// </summary>
public static bool ConcurrentTaskExecution { get; set; }
}
}
26 changes: 22 additions & 4 deletions CefSharp/Internals/MethodRunnerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,29 @@ private void ConsumeTasks()
{
try
{
while (!cancellationTokenSource.IsCancellationRequested)

if(CefSharpSettings.ConcurrentTaskExecution)
{
var task = queue.Take(cancellationTokenSource.Token);
task.RunSynchronously();
OnMethodInvocationComplete(task.Result);
//New experimental behaviour that Starts the Tasks on TaskScheduler.Default
while (!cancellationTokenSource.IsCancellationRequested)
{
var task = queue.Take(cancellationTokenSource.Token);
task.ContinueWith((t) =>
{
OnMethodInvocationComplete(t.Result);
}, cancellationTokenSource.Token);
task.Start(TaskScheduler.Default);
}
}
else
{
//Old behaviour, runs Tasks in sequential order on the current Thread.
while (!cancellationTokenSource.IsCancellationRequested)
{
var task = queue.Take(cancellationTokenSource.Token);
task.RunSynchronously();
OnMethodInvocationComplete(task.Result);
}
}
}
catch (OperationCanceledException)
Expand Down

0 comments on commit 767b478

Please sign in to comment.