简体   繁体   中英

How to wait for a callback without blocking main thread

I need to use a WebView in a custom renderer for Android and need to get a Value with EvaluateJavascript:

class ValueCallback : Java.Lang.Object, IValueCallback
{
    public Java.Lang.Object Value
    {
        get;
        set;
    }

    public void OnReceiveValue(Java.Lang.Object value)
    {
        Value = value;
    }
}

This is called from a Method in the Renderer, which in Turn gets called from a Property like Duration

public double Duration;

Get Duration from Webview:

var callback = new ValueCallback();
View.EvaluateJavascript(jsToExecute, callback);
//How to wait here without blocking the MainThread?

The Problem is that EvaluateJavascript can only be called from the MainThread and the Property is also called from MainThread. So when I use something like AutoResetEvent to wait for the Script to finish, the result is a Deadlock.

You're asking how to block the main thread without blocking the main thread. The answer is quite simple, you can't .

If you don't want to block the main thread you're going to need to not wait , and instead do whatever you want to have happen after the callback executes in the callback rather than waiting for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM