Cefsharp: Feature Request - Async Javascript Binding Add support for returning Task

Created on 6 May 2019  路  4Comments  路  Source: cefsharp/CefSharp

  • What version of the product are you using?
    73.1.130

  • What architecture x86 or x64?
    System architecture - x64, problem exists on both program architectures

  • On what operating system?
    Win10

  • Are you using WinForms, WPF or OffScreen?
    WPF

  • What steps will reproduce the problem?
    Bind a simple object, which has an async method. Run the method from Javascript.
    https://gist.github.com/perf2711/e96e69abad24c254d0192e88eb705709
    I hosted the index.html file through python3 -m http.server 8000.

  • What is the expected output? What do you see instead?

Synchronous methods work as they should, as well waiting for result from an asynchronous method in synchronous context. When running an async method, nothing is returned, promise never resolves and the object "locks up", preventing any additional calls.

This code was run inside the developer console.

>await TestBoundObject.syncMethod()
"SyncMethod"
>await TestBoundObject.syncMethod1()
"AsyncMethod1"
>await TestBoundObject.syncMethod2()
"AsyncMethod2"
>await TestBoundObject.syncMethod3()
"AsyncMethod3"
>await TestBoundObject.asyncMethod1()
>await TestBoundObject.asyncMethod2()
>await TestBoundObject.asyncMethod3()
>await TestBoundObject.syncMethod()
  • Please provide any additional information below.
    No exceptions are thrown. Breakpoints in each function are triggered.
    I don't see any information regarding not using async methods in bound objects in General usage, however, there are no examples of such case.

  • Does this problem also occur in the CEF Sample Application from http://opensource.spotify.com/cefbuilds/index.html?
    No, at least not in the Javascript Binding test. Anyway I think this issue cannot be reproduced in the sample app.

enhancement feature-request pr-merged

Most helpful comment

Support has been added in https://github.com/cefsharp/CefSharp/pull/2771 There are some QUnit test cases, please submit a PR with any failing cases that need to be addressed.

You need to set the follow before creating your ChromiumWebBrowser instances. If you attempt to return a Task without enabling the following then you should get a detailed error message.

THIS FEATURE IS ONLY AVAILABLE WHEN REGISTERING AN OBJECT AS ASYNC e.g. isAsync: true

```c#
CefSharpSettings.ConcurrentTaskExecution = true;

#### Example

```c#
public async Task<string[]> AsyncDownloadFileAndSplitOnNewLines(string url)
{
    var webClient = new WebClient();
    var download = await webClient.DownloadStringTaskAsync(new Uri(url));

    var lines = download.Split('\n').Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();

    return lines;
}
const url = "https://raw.githubusercontent.com/cefsharp/CefSharp/master/.editorconfig";

boundAsync.asyncDownloadFileAndSplitOnNewLines(url).then(function (lines)
{
    //Do something with the result.
});

All 4 comments

There are no examples as it's not currently support, the name is slightly misleading in this instance. Only the JavaScript side of the equation is actually asynchronous. It's difficult to implement this feature, you are welcome to submit a pull request.

The current supported method of executing long running tasks is to use a callback see https://github.com/cefsharp/CefSharp/blob/cefsharp/73/CefSharp.Example/JavascriptBinding/BoundObject.cs#L58

Had a quick look and it occurred to me that the CefSharpSettings.ConcurrentTaskExecution feature could be rewritten to support this without drastic changes.

A very rough implementation is at https://github.com/amaitland/CefSharp/commit/a9f20aceb7d78b095d542b96c2f7565424bc8c3c

No where near production ready.

Right, I thought it was supported, and I did something wrong.

However, it's odd that everything locks up when an async method is invoked. Shouldn't there be any warning/exception when it occurs?

Support has been added in https://github.com/cefsharp/CefSharp/pull/2771 There are some QUnit test cases, please submit a PR with any failing cases that need to be addressed.

You need to set the follow before creating your ChromiumWebBrowser instances. If you attempt to return a Task without enabling the following then you should get a detailed error message.

THIS FEATURE IS ONLY AVAILABLE WHEN REGISTERING AN OBJECT AS ASYNC e.g. isAsync: true

```c#
CefSharpSettings.ConcurrentTaskExecution = true;

#### Example

```c#
public async Task<string[]> AsyncDownloadFileAndSplitOnNewLines(string url)
{
    var webClient = new WebClient();
    var download = await webClient.DownloadStringTaskAsync(new Uri(url));

    var lines = download.Split('\n').Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();

    return lines;
}
const url = "https://raw.githubusercontent.com/cefsharp/CefSharp/master/.editorconfig";

boundAsync.asyncDownloadFileAndSplitOnNewLines(url).then(function (lines)
{
    //Do something with the result.
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amaitland picture amaitland  路  3Comments

looselive picture looselive  路  4Comments

avspeed picture avspeed  路  6Comments

SimonTheCat picture SimonTheCat  路  3Comments

Spocher picture Spocher  路  4Comments