简体   繁体   English

如何在不阻塞主线程的情况下等待回调

[英]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: 我需要在Android的自定义渲染器中使用WebView,并且需要使用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 这是从Renderer中的Method调用的,它在Turn中从一个属性调用,如Duration

public double Duration;

Get Duration from Webview: 从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. 问题是只能从MainThread调用EvaluateJavascript,并且还从MainThread调用Property。 So when I use something like AutoResetEvent to wait for the Script to finish, the result is a Deadlock. 因此,当我使用类似AutoResetEvent之类的东西等待脚本完成时,结果就是死锁。

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. 如果您不想阻止主线程,您将不需要等待 ,而是在回调回调中执行之后执行您想要发生的任何事情,而不是等待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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