Skip to content

Commit

Permalink
Core - MethodRunnerQueue use TaskCreationOptions.HideScheduler to
Browse files Browse the repository at this point in the history
To prevent subtasks from attempting to use our LimitedConcurrencyLevelTaskScheduler we need to
specify TaskCreationOptions.HideScheduler when creating the Task

Resolves #3293
  • Loading branch information
amaitland committed Nov 25, 2020
1 parent 40fc9f8 commit 80753eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
23 changes: 23 additions & 0 deletions CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ public int Div(int divident, int divisor)
return divident / divisor;
}

public string DivWithBlockingTaskCall(int dividend, int divisor)
{
var taskToWaitOn = ExecuteTaskBeforeDivision(dividend, divisor);
taskToWaitOn.Wait();
return taskToWaitOn.Result;
}

private async Task<string> ExecuteTaskBeforeDivision(int dividend, int divisor)
{
await RunAsync();
return (dividend / divisor).ToString();
}

private Task RunAsync()
{
return Task.Run(() => Run());
}

private void Run()
{
Debug.WriteLine("AsyncBoundObject Run execution.");
}

public string Hello(string name)
{
return "Hello " + name;
Expand Down
14 changes: 14 additions & 0 deletions CefSharp.Example/Resources/BindingTestAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@
});
});

QUnit.test("Async call (Div with Blocking Task 16 / 2):", function (assert)
{
var asyncCallback = assert.async();

boundAsync.divWithBlockingTaskCall(16, 2).then(function (actualResult)
{
const expectedResult = 8

assert.equal(expectedResult, actualResult, "Divide 16 / 2 resulted in " + expectedResult);

asyncCallback();
});
});

QUnit.test("Async call (Divide 16 /0)", function (assert)
{
var asyncCallback = assert.async();
Expand Down
6 changes: 3 additions & 3 deletions CefSharp/Internals/MethodRunnerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class MethodRunnerQueue : IMethodRunnerQueue
{
//Limit to 1 task per methodRunnerQueue
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/d0bcb415-fb1e-42e4-90f8-c43a088537fb/aborting-a-long-running-task-in-tpl?forum=parallelextensions
private readonly TaskFactory methodRunnerQueueTaskFactory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(1));
private readonly LimitedConcurrencyLevelTaskScheduler taskScheduler = new LimitedConcurrencyLevelTaskScheduler(1);
private readonly JavascriptObjectRepository repository;
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

Expand All @@ -32,7 +32,7 @@ public void Dispose()

public void Enqueue(MethodInvocation methodInvocation)
{
methodRunnerQueueTaskFactory.StartNew(() =>
Task.Factory.StartNew(() =>
{
var result = ExecuteMethodInvocation(methodInvocation);

Expand All @@ -41,7 +41,7 @@ public void Enqueue(MethodInvocation methodInvocation)
{
handler(this, new MethodInvocationCompleteArgs(result));
}
}, cancellationTokenSource.Token);
}, cancellationTokenSource.Token, TaskCreationOptions.HideScheduler, taskScheduler);
}

private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)
Expand Down

0 comments on commit 80753eb

Please sign in to comment.