简体   繁体   English

如何暂停线程一段时间,然后在用户交互时显示UI,继续线程执行

[英]How to pause the thread for some time and then show the UI once user interacts, continue the thread execution

I have started the Thread, In that thread i am trying to connect to the server, After receiving the response, I have to update the UI with event listeners(implemented through Interface). 我已经启动了线程,在该线程中我正在尝试连接到服务器,在收到响应后,我必须使用事件侦听器(通过接口实现)更新UI。 Here After receiving the response i need to show the popup Dialog once user clicks OK, need to continue the thread and complete the other process. 在接收到响应后,我需要在用户单击“确定”后显示弹出对话框,需要继续该线程并完成其他进程。

 class ConnectionThread extends Thread {
        ConnectionThread() {
            this.setName("ConnectionThread");
        }

        @Override
        public void run() {
        // Need to pause the thread for sometime, Need to do the functionality here.  
     ((Activity)mContext).runOnUiThread(new Runnable() {
                public void run() {
            // custom dialog
               showAlertDialog();  
               // start the thread functionality again from that position.  
 }
});

} }

I have tried with wait() concept and also join, which are not helped as expected. 我已尝试使用wait()概念并加入,这些都没有按预期帮助。 Any help appreciated. 任何帮助赞赏。

you can use countdownlatch 你可以使用countdownlatch

class ConnectionThread extends Thread {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        public ConnectionThread() {
            this.setName("ConnectionThread");
        }

        @Override
        public void run() {
            try {
                sleep(2000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //update ui then
                        countDownLatch.countDown();
                    }
                });
                countDownLatch.await();
                //start process again
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

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

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