开发者

Can I run one SwingWorker within another?

开发者 https://www.devze.com 2023-01-10 18:33 出处:网络
I need to run two SwingWorkers.One of them can only run after the other is done.Can I run them like this?

I need to run two SwingWorkers. One of them can only run after the other is done. Can I run them like this?

class TestWorker {
    private FirstWorker worker1;
    private SecondWorker worker2;

    public TestWorker() {
        worker1 = new FirstWorker() {
            @Override
           开发者_如何学Go protected void done() {
                try {
                    result1 = get();
                } catch (Exception) {
                    // exception handling
                }

                worker2 = new SecondWorker() {
                    @Override
                    protected void done() {
                        try {
                            result2 = get();
                        } catch (Exception) {
                            // exception handling
                        }
                    }
                }

                worker2.execute();
            }
        }

        worker1.execute();
    }
}

And how should I cancel them? Like this?

private cancel() {
    if (worker2 != null) work2.cancel();
    if (worker1 != null) work1.cancel();
}

Thanks a lot!


You can do it that way and it will work. However, unless there are other operations in your outer done that you're not showing, you would probably be better off with something that did both operations in doInBackground and returned an array of the results.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号