暂停SwingWorker [英] Pausing SwingWorker

查看:196
本文介绍了暂停SwingWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是GUI和多线程的初学者.我目前有一个模拟,该模拟通过控制台中的一个错误运行.我希望能够使用按钮暂停该错误.我有两个按钮(运行和暂停),运行按钮将启动仿真,并且暂停按钮应将其暂停(或使其休眠一小段时间)香港专业教育学院设法使运行按钮起作用,但是一旦其运行,我便无法单击暂停运行(因为我相信它在同一个线程中)我已经读了很多,但仍然似乎无法解决..任何帮助将不胜感激. //在我的行动倾听者"中.

Im a beginner to GUI and multithreading. I currently have a simulation which runs through a bug moving about in the console. I want to be able to pause the bug using a button. I have two buttons (run and pause) the run button will start the simulation and the pause button should pause it ( or make it sleep for a bit) ive managed to get the run button working but i am then unable to click pause once its running (because its in the same thread i believe) Ive read into it alot but still cant seem to work it out.. any help would be massively appreciated.. //IN MY ACTION LISTENER..

else if (E.getSource() == Pause) {
        Worker pauseWorker = new Worker();
        pauseWorker.execute();

在我的新工人班上

import javax.swing.SwingWorker;

   public class Worker extends SwingWorker<Void, Void> {

@Override
protected Void doInBackground() throws Exception {
    // System.out.println("Background");
    for (int i = 0; i <= 1; i++) {
        Thread.sleep(1000);
        System.out.println("Background running");
    }

    return null;

}

}

推荐答案

else if (E.getSource() == Pause) {
    Worker pauseWorker = new Worker();
    pauseWorker.execute();

这将启动一个新的工作程序,不会停止正在运行的工作程序.

This starts a new worker, does not stop the running one.

相反,您可以保留对后台工作者的引用,而在按下暂停按钮时可以引用cancel().请参见 SwingWorker.cancel()

Instead, you can keep a reference to the background worker and cancel() it when the pause button is pressed. See SwingWorker.cancel()

else if (E.getSource() == Pause) {
    worker.cancel(true);
}

在工人阶级中,定期检查您是否已被取消:

And in the worker class, regularly check if you've been cancelled:

@Override
protected Void doInBackground() throws Exception {
    // System.out.println("Background");
    while(!isCancelled()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("Background interrupted");
        }
        System.out.println("Background running");
    }
    return null;
}


如果确实需要暂停而不是取消工作程序,则必须编写自己的pause()方法并自行进行管理.


If you really do need to pause rather than cancel the worker, you'll have to write your own pause() method and do the administration yourself.

为了给您一些想法,然后将类似这样的内容输入worker类:​​

To give you some idea, something like this goes into the worker class then:

boolean paused = false;

public void pause() {
    paused = true;
}

public synchronized void resume() {
    paused = false;
    this.notify();
}

@Override
protected Void doInBackground() throws Exception {
    while(!isCancelled()) {
        if( paused ) {
            System.out.println("Background paused, waiting for resume");
            try {
                synchronized(this){
                    wait(1000);
                }
            } catch (InterruptedException ex) {
                System.out.println("Background interrupted");
            }
        } else {
            System.out.println("Background running");
            // do a chunk of actual work
        }
    }
    return null;
}

这篇关于暂停SwingWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆