The recommended way to run AnAction async

Every time I execute my AnAction from the MainMenu the whole screen get stuck untill the action is finished and the menu stays open.

I tried wrapping my code with something like that:

                         ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {                     @Override                     public void run() {                          EventQueue.invokeLater(new Runnable() {                               @Override                               public void run() {


But seems it doesnt help.

Any advice?

Thanks
4 comments

If your action runs in the event dispatch thread, it will by definition block the UI. If you remove EventQueue.invokeLater() from your code snippet, the action will run in a background thread and will not block the UI.

0

Is that really correct?

(I'm asking because I'm going to need to do this soon in my own plugin).

Wouldn't a better answer be that you need to move the long running part of the code to before the invoke later, and make sure that you only touch the GUI (ie. for display the output of the long running action) in the invoke later section?

Or is there some IDEA magic going on here that means you can touch the GUI from a background thread?

0

There is no IDEA magic; the UI still needs to be updated from the event dispatch thread. The original poster didn't say what he was doing in his action code and whether he needed to touch the UI.

0

Thanks Dmitry.
I did touch the UI in there.

It works your way.

Many thanks.

0

Please sign in to leave a comment.