简体   繁体   English

如何暂停/休眠Java程序,直到单击按钮

[英]How to pause/sleep Java Program until a button click

I have created a Java Application which is a screen recorder. 我创建了一个Java应用程序,它是一个屏幕录像机。 First I added functionality for Start and Stop recording but now asked to add PAUSE option too. 首先,我添加了“开始”和“停止”记录的功能,但现在又要求添加“暂停”选项。 I want to the entire working to stop when Pause is clicked and resume when clicked again. 我希望整个工作在单击“ 暂停”时停止,并在再次单击时恢复。 I am unable to use sleep because it requires time. 我无法使用睡眠,因为它需要时间。

I have tried wait-notify logic. 我尝试了等待通知逻辑。 Can someone please guide me, how can I stop working of code until clicked again? 有人可以指导我,我如何才能停止工作代码,直到再次单击?

boolean paused;
    //boolean paused=false;
    @FXML
    private void pauseRec(ActionEvent event) throws AWTException, InterruptedException
    {
        if (paused==false)
        {
            paused= true;
            pauseRec.setText("Resume");
            recordStateLabel.setText("Recording Paused");
            synchronized(threadObject)
            {
                // Pause
                try 
                {
                    threadObject.wait();
                } 
                catch (InterruptedException e) 
                {
                }
            }
            //rob.delay(10000);
        }
        else if(paused ==true)
        {
            paused=false;
            pauseRec.setText("Pause");
            recordStateLabel.setText("Recording..");
            synchronized(threadObject)
            {
                threadObject.notify();
            }
        }
    }
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> System.out.println("Printed after 1 second from FX thread")));
timeline.play();

So this will pause the current working for 1 sec duration and will resume afterwards. 因此,这会将当前工作暂停1秒钟,然后再恢复。 For your case you can make this function to run for finite time and then resume as the resume button is clicked. 对于您的情况,您可以使此功能运行有限的时间,然后在单击恢复按钮时恢复运行。

Follow this link for more details 点击此链接以获取更多详细信息

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

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