简体   繁体   English

如何在Xamarin Android中等待处理程序线程完成?

[英]How to Wait for Handler Thread to finish in Xamarin Android?

How to wait for the handler.PostDelayed to finish before the function continues? 在函数继续之前如何等待handler.PostDelayed完成?

    int num = null;
    public int DoSomething()
    {

            var handler = new Handler();
            handler.PostDelayed(() => StartCapital(), 10);

            handler.PostDelayed(() =>
            {
                    num = StartGain();
            }, 300);

           return num;
     }

how to Wait for the StartGain() Method thread to finish? 如何Wait StartGain()方法线程完成? the method takes sometime to finish and the Function it is in returns null 该方法需要一些时间才能完成,并且它所在的函数返回null

This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses, 在理解线程的工作方式时,这似乎是一个问题。您创建了一个后台线程,这样您就不必为了完成许多其他用途而完成一些巨大的过程而阻塞主线程,

Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. 就像您的情况一样,方法StartGain()将在延迟300毫秒后在主线程上执行,因此,如果您不想等待,请删除延迟和处理程序,然后以正常流程运行它。 If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing. 如果该方法返回的值需要进一步的计算,则在继续之前,请在单独的后台线程中运行它。

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

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