diff --git a/CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs b/CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs index 090adfa69..745917c48 100644 --- a/CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs +++ b/CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs @@ -31,6 +31,29 @@ namespace CefSharp.Example.JavascriptBinding return divident / divisor; } + public string DivWithBlockingTaskCall(int dividend, int divisor) + { + var taskToWaitOn = ExecuteTaskBeforeDivision(dividend, divisor); + taskToWaitOn.Wait(); + return taskToWaitOn.Result; + } + + private async Task 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; diff --git a/CefSharp.Example/Resources/BindingTestAsync.js b/CefSharp.Example/Resources/BindingTestAsync.js index 9a3711f56..f14728d73 100644 --- a/CefSharp.Example/Resources/BindingTestAsync.js +++ b/CefSharp.Example/Resources/BindingTestAsync.js @@ -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(); diff --git a/CefSharp/Internals/MethodRunnerQueue.cs b/CefSharp/Internals/MethodRunnerQueue.cs index e6d21d55c..2f703ed3f 100644 --- a/CefSharp/Internals/MethodRunnerQueue.cs +++ b/CefSharp/Internals/MethodRunnerQueue.cs @@ -13,7 +13,7 @@ namespace CefSharp.Internals { //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(); @@ -32,7 +32,7 @@ namespace CefSharp.Internals public void Enqueue(MethodInvocation methodInvocation) { - methodRunnerQueueTaskFactory.StartNew(() => + Task.Factory.StartNew(() => { var result = ExecuteMethodInvocation(methodInvocation); @@ -41,7 +41,7 @@ namespace CefSharp.Internals { handler(this, new MethodInvocationCompleteArgs(result)); } - }, cancellationTokenSource.Token); + }, cancellationTokenSource.Token, TaskCreationOptions.HideScheduler, taskScheduler); } private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)