-3

So, I figured out the SwingWorker thing.

However, another problem emerged (go figure)...

Swing worker acctually manages error reporting and email sending, and emailing being a lenghty (more than .5 seconds) task, it comes in handy...

In order to prevent my program to continu executing before the error has been processed, I have to pause the EDT thread (with that syncronised thingy). However, that will also pause cool little animation that indetermined JProgressBar has, and which is being used while the message is sent, so, when the EDT is paused.

My question is, is there any way to stop my program from continuing to execute, and at the same time dispaly the animation on JProgressBar?

Here is somwhat pseudo code:

Main class error occurrs - pauses thread with syncronised and executes (creates) another class that executes SwingWorker (that another class is neccessary, it really is, if it weren't I wouldn't be having so much trouble with this).

new ErrorDialog(Main.modal, lang.getString("errorConfigMissingTitle"), lang.getString("errorConfigMissingMessage"), e, false);

It invokes this:

            //bunch of code, this below is an action listener of one of the buttons

            String s = "";

            (new ErrorSender(parent, error, s, dialog)).execute();

            synchronized (s)
            {
                try
                {
                    s.wait();
                }

                catch (InterruptedException e1)
                {
                    e1.printStackTrace();
                }
            }

            dialog.dispose();

            //create JDialog that tell user message was sent

SwingWorker class creates sendingErrorReport JDialog (with JProgressBar) and sends the message in the background, when message is sent, removes pause (notyfyAll()).

public class ErrorSender extends SwingWorker<Boolean, Void>
{
    Exception e;
    String s;

    public ErrorSender(JFrame parent, Exception error, String k, JDialog sendingDialog)
    {
        e = error;
        s = k;

        sendingDialog = new JDialog(parent, "Sending...", false);

        JProgressBar progress = new JProgressBar();
        progress.setString("Sending report...");
        progress.setStringPainted(true);
        progress.setIndeterminate(true);

        sendingDialog.getContentPane().add(progress);   
        sendingDialog.pack();
        sendingDialog.setLocationRelativeTo(null);
        sendingDialog.getContentPane().validate();
        sendingDialog.setResizable(false);
        sendingDialog.setVisible(true);
    }

    @Override
    protected Boolean doInBackground() throws Exception
    {

    //send email here

        synchronized (s)
        {
            s.notifyAll();
        }

        return null;
    }
}

I'm not removing pause from done() method because, if pause is not removed, done() is never reached/called...

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • [your last choice](http://stackoverflow.com/questions/8169964/is-mvc-in-swing-thread-safe) – mKorbel Sep 28 '12 at 21:58
  • It would help if you explained the overall effect you are trying to achieve - I feel like I have jumped in halfway through an explanation...? You shouldn't need to pause the EDT to prevent your application from doing stuff during a long-running operation. – DNA Sep 28 '12 at 22:07

2 Answers2

4

Pausing the EDT while the background thread is running and then resuming it is exactly the same, but in a more convoluted way, as doing the lengthy, blocking operation in the EDT. It makes no sense.

If you want to prevent users from using the program while the background task is running, simply display a modal dialog, and close it (or allow closing it) once the background task is done.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 4
    Execute the `SwingWorker` first, then display the dialog as demonstrated in my example to your [last question](http://stackoverflow.com/questions/12644778/multithreading-issues-with-swing-around-dialog-create-destroy/12647582#12647582) – MadProgrammer Sep 28 '12 at 21:47
1

have look and please to test

  1. booking example SwingWorker and JProgressBar

  2. Executor and SwingWorker

  3. how to get the exception

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319