问题
I have a test button in a Dialog, when user click that button, my application will try to connect to a database via JDBC.
For some testing cases this job will take a long while to give response to user, network connection timeout for instance. I do this by attaching a listener to that button and connect to database in that listener. when connecting, the user interface will not response until the connecting job is finished. Any user action ocurrs while connecting will take effect after connecting job done.
For example: there is another "cancel" button in that dialog, click this button will close the dialog. if you click "cancel" button while a connecting job is running, the dialog will not be closed until connecting job is done!
I hope user actions happened while my connecting job is running will be droped, not stored and take effect later.
I am using eclipse indigo on windows 7
My question should be this :
How to ignore any user's action happened while running a job in background.
回答1:
The two ways I found to ignore any user actions while a job runs in the background so far:
The first one is to call shell.setEnabled(false)
before the operation starts and shell.setEnabled(true)
after the operation is done.
(optional) combined with the BusyIndicator
to give the user some feedback.
The second one is using a modal dialog with no Buttons so the user cannot close it (styles e.g SWT.APPLICATION_MODAL | SWT.SHEET | SWT.NO_TRIM
). The dialog holds a Label e.g "Loading ..." or a ProgressBar
. When using RCP the ProgressMonitorDialog
is an option.
In my case I dont want to use the second solution because I dont know how long the background job runs. If the job is very short I want to avoid some flickering when the ProgressMonitorDialog open and close in rapid succession.
@screentiger.com I did not see in the article you shared how you ignore any user actions while a job runs in the background. I would appreciate if you could be more specific and maybe give a code example.
回答2:
there is only one UI thread in swt (also most others), your programming way blocks the UI.
For your job, you need to create a new thread to do it and don't so such long term job in UI thread, use the executor introduce in 1.5.
the best way is to use the eclipse Job mechanism. You can start learn from: http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html I
来源:https://stackoverflow.com/questions/6660574/eclipse-rcp-how-to-block-ui-while-running-a-job-in-background