Make async synchronous event in JavaScript

I am using a WPF 3.5SP1 WebBrowser control to display a page containing some javascript features. Then my program needs to call a javascript function that will make an asynchronous call. I need a way to get the result of this asynchronous call in C # so that I can process the result.

Is there a way that the javascript function can work until something happens (without blocking the browser)?

edit: I already use a callback - the second function is actually called "some-async-function-complete". It is called when the async event completes. Now I need a way to get the result in C #.

For further clarification: C #

var result = WebBrowser.InvokeScript("myscript")

Javascript

var result;

function myscript()
{
some-async-function();
/* what goes here? */
/* wait until result != null */
return result;
}

function some-async-function-complete(retval)
{
result = retval;
}
+3
5

javascript - ( )?

- , : . # JS-, , -.


- WinForms WebBrowser, WPF, :

[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Callback
{
   // allows an instance of Callback to look like a function to the script
   // (allows callback() rather than forcing the script to do callback.callMe)
   [System.Runtime.InteropServices.DispId(0)]
   public void callMe(string url)
   {
      // whatever you want to happen once the async process is complete
   }
}

...

Callback cb = new Callback();
WebBrowser.InvokeScript("myscript", new object[] { cb })

...

function myscript(callback)
{
   some_async_function(function()
   {
      // script-specific completion code
      if ( callback )
         callback();
   });
}
+4

. : javascript?

:

JavaScript JavaScript .

:

JavaScript - JavaScript . .

, , .

+2

, ... , ...

function myscript()
{
    some-async-function(
        params, //any params you plan to use
        function(result) {
           //handle the result here
        });
}

function some-async-function(retval,callback)
{
    //assuming this really is an async function such
    //as an ajax call, etc...
    ...

    //work done, use the call back
    callback(retval);

}
0

, . # , . -.

, , - javascript ajax .

0

, javascript , - ( )?

, , .

AFAIK, - JavaScript, .

I think you will need to send JavaScript resultto the server using Ajax (from some-async-function-complete). Then break your C # into one part call myscript, and the other answer and process result.

0
source

Source: https://habr.com/ru/post/1702795/


All Articles