• Post Reply Bookmark Topic Watch Topic
  • New Topic

SWT-Disable event while performing long running task

 
nikolas Kris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have in my application a button that when it is pressed, runs a long running task (usually takes a couple of seconds....).

My problem is that while the task runs, I want to stop the button from receiving clicks. Unfortunately this is something I have not achieved.

I have tried creating a new thread and calling the long running method, and using the Display.geCurrent().asyncExec...

Examples of what I have tried so far:


Additionally tried like that:


In both cases, while the button is disabled and the cursor busy, if the user clicks the button, the long running method is ran several times - and this is what I want to avoid.

I am stuck in one of those "WTF?" situations. I really appreciate your help
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi n,

I know nothing about SWT, so what follows may be completely irrelevant.

First of all, disabling a lightweight component in Swing does not prevent
mouseclicks being reported. So my advice would be to create a separate
variable of type SelectionListener that you add to the button*. The code
should contain something like

A click on the button now removes the listener from the button.

* instead of the anonymous class that you are using now

Then, if I think of the SwingWorker class, you have the method 'done()'.
This method is invoked when the SwingWorker is finished, and it is invoked
on the EDT. So that would be an ideal place to re-install the listener to the
button and enable the button again.

As said, if SWT has something similar, I don't know.

Greetz,
Piet
 
nikolas Kris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Thanks for your suggestion. I had already tried it,but didn't work with similar to the above implementations.
I have found a solution I think, combining your way (removing the event listener) and running everything in the same thread. I am testing it now, but seems to work.

So all in all, what seems to be the solution is:

1) remove the event listener
2) call the long running method
3) re-register the event listener

do all the above in the same thread.

Best,

N.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Piet, I haven't dabbled in SWT, but another approach to this kind of problem is to use a boolean flag as an instance field.
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nikolas,

I just found some SWT info, at
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fevents%2Fpackage-summary.html


This example is given on that page:


From this description, It seems to me that this code must be called from within
a long running code. So, if I'm right, I think you should issue,
at the end of the 'myLongRunningMethod()' something like



What Daaryl writes is also simple, although I do not know when this "longTaskIsRunning"
is set to false again. Either way: does your GUI not get blocked?

Greetz,
Piet
 
nikolas Kris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thank you for your suggestions. Unfortunately no matter how much I experimented i did not succeed in making it work perfectly.
I have only recently started working with swt and I am surprised that there is no clear and simple way to achieve what should be expected a standard behavior...
I will keep the thread open and will be looking for a clear answer on how do do that. I hope an swt expert out there posts a simple working example - so that it is somewhere documented.

Just for the records - in order to overcome the issue, I changed my gui completely: NO button, I initiate the long running method with other event.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!