Updating a Progress Bar and Detecting a User Interrupt (Visual J++)

Take the following steps to update a progress bar during processing and detect a user interrupt:

1. Start with the project that you created in Loading and Displaying an Image.

2. image\btncmd.gif Select the Button control; then add the control to your main form. Put the control at the top of the form to keep it away from the image.

3. In the Properties box, change the Button control's Text property to Do Median.

4. image\btncmd.gif Select the Button control; then add another control to your main form. Put the control at the top of the form to keep it away from the image.

5. In the Properties box, change the Button control's Text property to Quit.

6. Select the Progress Bar control then add it to your main form. Put the control at the top of the form next to the Quit button.

7. Add the following private member variables:

private boolean m_bEscape = false;  // Use to indicate the user interrupt
private boolean m_bInProc = false;  // Use to avoid closing the form during processing

8. Add the following code to the first Button control's Click event:

private void button1_click(Object source, Event e)
{
   // Enable the ProgressStatus event
   LEAD1.setEnableProgressEvent( true );

   // Initialize the indicators
   m_bEscape = false;  // The user does not want to quit
   m_bInProc = true;  // Processing is taking place

   // Perform a relatively slow median filter
   LEAD1.Median( (short) 4 );

   // Clean up
   m_bInProc = false;  // Processing is no longer taking place
   LEAD1.ForceRepaint();
}

9. Add the following code to the Lead1 control's progressStatus event:

private void LEAD1_progressStatus(Object source, LTOCXU.LEAD.ProgressStatusEvent e)
{
   Application.doEvents();  // Detect Windows events

   if( !m_bEscape )  // Look for the Click on the Quit button
      progressBar1.setValue( e.iPercent ); 
   else
      LEAD1.setEnableProgressEvent( false );  // Cancel the task
}

10. Add the following code to the second Button control's Click event:

private void button2_click(Object source, Event e)
{
   m_bEscape = true;  // The user wants to quit
   progressBar1.setValue( 0 );  // Set the progress bar back to the beginning
}

11. Run your program to test it.