Starting “asyncExec” in Mouse Down Event results in blocking behavior
up vote
1
down vote
favorite
Having a "next" Button, when I press keyboard enter key with the button selected, the widgetSelected event of the buton is being repeatedly called once and once and doing a super fast next. It's exactly the behaviour I want, but only happens with keyboard enter key. I want to have that behaviour with mouse click when holding the click. When trying to do the same with the mouse click, the behaviour is not the same, it only makes one event call, and when the click is end (UP).
How to simulate the same behaviour with the mouse click?
I tryed it doing this, but it blocks the UI (can't understand why) and mouseUp is never being called, it blocks forever in the while
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
System.out.println("mouseDown");
mouseDown = true;
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
while (mouseDown) {
System.out.println("Doing next in mouseDown");
next(composite, label_1);
synchronized(this){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
}
@Override
public void mouseUp(MouseEvent e) {
System.out.println("mouseUp");
mouseDown = false;
}
});
java multithreading asynchronous swt mouseevent
add a comment |
up vote
1
down vote
favorite
Having a "next" Button, when I press keyboard enter key with the button selected, the widgetSelected event of the buton is being repeatedly called once and once and doing a super fast next. It's exactly the behaviour I want, but only happens with keyboard enter key. I want to have that behaviour with mouse click when holding the click. When trying to do the same with the mouse click, the behaviour is not the same, it only makes one event call, and when the click is end (UP).
How to simulate the same behaviour with the mouse click?
I tryed it doing this, but it blocks the UI (can't understand why) and mouseUp is never being called, it blocks forever in the while
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
System.out.println("mouseDown");
mouseDown = true;
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
while (mouseDown) {
System.out.println("Doing next in mouseDown");
next(composite, label_1);
synchronized(this){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
}
@Override
public void mouseUp(MouseEvent e) {
System.out.println("mouseUp");
mouseDown = false;
}
});
java multithreading asynchronous swt mouseevent
Start by following the 'blocking' issue. Also, the variable must bevolatile
to ensure it is "seen" between the threads, should there be multiple threads.
– user2864740
Nov 9 at 20:03
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
I don't have a proposal other than usingvolatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.
– user2864740
Nov 9 at 20:47
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Having a "next" Button, when I press keyboard enter key with the button selected, the widgetSelected event of the buton is being repeatedly called once and once and doing a super fast next. It's exactly the behaviour I want, but only happens with keyboard enter key. I want to have that behaviour with mouse click when holding the click. When trying to do the same with the mouse click, the behaviour is not the same, it only makes one event call, and when the click is end (UP).
How to simulate the same behaviour with the mouse click?
I tryed it doing this, but it blocks the UI (can't understand why) and mouseUp is never being called, it blocks forever in the while
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
System.out.println("mouseDown");
mouseDown = true;
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
while (mouseDown) {
System.out.println("Doing next in mouseDown");
next(composite, label_1);
synchronized(this){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
}
@Override
public void mouseUp(MouseEvent e) {
System.out.println("mouseUp");
mouseDown = false;
}
});
java multithreading asynchronous swt mouseevent
Having a "next" Button, when I press keyboard enter key with the button selected, the widgetSelected event of the buton is being repeatedly called once and once and doing a super fast next. It's exactly the behaviour I want, but only happens with keyboard enter key. I want to have that behaviour with mouse click when holding the click. When trying to do the same with the mouse click, the behaviour is not the same, it only makes one event call, and when the click is end (UP).
How to simulate the same behaviour with the mouse click?
I tryed it doing this, but it blocks the UI (can't understand why) and mouseUp is never being called, it blocks forever in the while
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
System.out.println("mouseDown");
mouseDown = true;
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
while (mouseDown) {
System.out.println("Doing next in mouseDown");
next(composite, label_1);
synchronized(this){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
}
@Override
public void mouseUp(MouseEvent e) {
System.out.println("mouseUp");
mouseDown = false;
}
});
java multithreading asynchronous swt mouseevent
java multithreading asynchronous swt mouseevent
edited Nov 9 at 20:02
user2864740
43.3k669142
43.3k669142
asked Nov 9 at 19:55
NullPointerException
12.7k52159275
12.7k52159275
Start by following the 'blocking' issue. Also, the variable must bevolatile
to ensure it is "seen" between the threads, should there be multiple threads.
– user2864740
Nov 9 at 20:03
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
I don't have a proposal other than usingvolatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.
– user2864740
Nov 9 at 20:47
add a comment |
Start by following the 'blocking' issue. Also, the variable must bevolatile
to ensure it is "seen" between the threads, should there be multiple threads.
– user2864740
Nov 9 at 20:03
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
I don't have a proposal other than usingvolatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.
– user2864740
Nov 9 at 20:47
Start by following the 'blocking' issue. Also, the variable must be
volatile
to ensure it is "seen" between the threads, should there be multiple threads.– user2864740
Nov 9 at 20:03
Start by following the 'blocking' issue. Also, the variable must be
volatile
to ensure it is "seen" between the threads, should there be multiple threads.– user2864740
Nov 9 at 20:03
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
I don't have a proposal other than using
volatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.– user2864740
Nov 9 at 20:47
I don't have a proposal other than using
volatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.– user2864740
Nov 9 at 20:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The Runnable
you give to asyncExec
runs in the UI thread. You must never do any sort of wait in the UI thread as that will block the UI until it completes.
So you cannot run a loop like this as it just blocks the UI. Since the loop never returns to the main SWT readAndDispatch
loop no UI actions are done.
Instead use the timerExec
method of Display
to schedule a Runnable
to run after a given interval. This runnable should do one step of the action and use timerExec
to schedule the next step later.
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
add a comment |
up vote
1
down vote
I remember there was another question a few days ago regarding a long mouse click behaviour but I can't find it anymore. I put this code based on greg-449 solution to use timerExec method, after my failed attempts to use asyncExec in the UI thread. :)
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
mouseDown = true;
Display.getCurrent().timerExec(1000, () -> {
if (mouseDown) {
button.notifyListeners(SWT.Selection, new Event());
button.notifyListeners(SWT.MouseDown, new Event());
}
});
}
@Override
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(
e -> System.out.println("Do next")));
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The Runnable
you give to asyncExec
runs in the UI thread. You must never do any sort of wait in the UI thread as that will block the UI until it completes.
So you cannot run a loop like this as it just blocks the UI. Since the loop never returns to the main SWT readAndDispatch
loop no UI actions are done.
Instead use the timerExec
method of Display
to schedule a Runnable
to run after a given interval. This runnable should do one step of the action and use timerExec
to schedule the next step later.
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
add a comment |
up vote
3
down vote
accepted
The Runnable
you give to asyncExec
runs in the UI thread. You must never do any sort of wait in the UI thread as that will block the UI until it completes.
So you cannot run a loop like this as it just blocks the UI. Since the loop never returns to the main SWT readAndDispatch
loop no UI actions are done.
Instead use the timerExec
method of Display
to schedule a Runnable
to run after a given interval. This runnable should do one step of the action and use timerExec
to schedule the next step later.
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The Runnable
you give to asyncExec
runs in the UI thread. You must never do any sort of wait in the UI thread as that will block the UI until it completes.
So you cannot run a loop like this as it just blocks the UI. Since the loop never returns to the main SWT readAndDispatch
loop no UI actions are done.
Instead use the timerExec
method of Display
to schedule a Runnable
to run after a given interval. This runnable should do one step of the action and use timerExec
to schedule the next step later.
The Runnable
you give to asyncExec
runs in the UI thread. You must never do any sort of wait in the UI thread as that will block the UI until it completes.
So you cannot run a loop like this as it just blocks the UI. Since the loop never returns to the main SWT readAndDispatch
loop no UI actions are done.
Instead use the timerExec
method of Display
to schedule a Runnable
to run after a given interval. This runnable should do one step of the action and use timerExec
to schedule the next step later.
answered Nov 9 at 21:06
greg-449
87.1k166196
87.1k166196
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
add a comment |
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
Hi @greg-449 thanks for your idea but how to achieve a bucle of timerExec methods until mouseUp event? If i call a new timerExec inside the original timerExec I'm getting only two calls to my function, and if I do timerExecs allways my funcion is called I'm getting a infinite loops of timerExecs.
– NullPointerException
Nov 9 at 22:54
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
ok @greg-449 I understand you now. You mean using recursive calls of a method which launches timerExec when my mouseDown boolean is true and setting it to false on mouseUp event. It worked using that. Thank you for your great idea :)
– NullPointerException
Nov 9 at 22:57
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
mmmm the problem is that if I'm doing that I'm getting superfast next even with one single click :| mmm I'm sure must be a easy way to solve this...
– NullPointerException
Nov 9 at 23:00
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
well I think I solved it using a new method which tries to start the 100ms method if the boolean is still true after 1 second, the behaviour is similar to what i was expecting so thank you so much
– NullPointerException
Nov 9 at 23:08
add a comment |
up vote
1
down vote
I remember there was another question a few days ago regarding a long mouse click behaviour but I can't find it anymore. I put this code based on greg-449 solution to use timerExec method, after my failed attempts to use asyncExec in the UI thread. :)
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
mouseDown = true;
Display.getCurrent().timerExec(1000, () -> {
if (mouseDown) {
button.notifyListeners(SWT.Selection, new Event());
button.notifyListeners(SWT.MouseDown, new Event());
}
});
}
@Override
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(
e -> System.out.println("Do next")));
add a comment |
up vote
1
down vote
I remember there was another question a few days ago regarding a long mouse click behaviour but I can't find it anymore. I put this code based on greg-449 solution to use timerExec method, after my failed attempts to use asyncExec in the UI thread. :)
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
mouseDown = true;
Display.getCurrent().timerExec(1000, () -> {
if (mouseDown) {
button.notifyListeners(SWT.Selection, new Event());
button.notifyListeners(SWT.MouseDown, new Event());
}
});
}
@Override
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(
e -> System.out.println("Do next")));
add a comment |
up vote
1
down vote
up vote
1
down vote
I remember there was another question a few days ago regarding a long mouse click behaviour but I can't find it anymore. I put this code based on greg-449 solution to use timerExec method, after my failed attempts to use asyncExec in the UI thread. :)
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
mouseDown = true;
Display.getCurrent().timerExec(1000, () -> {
if (mouseDown) {
button.notifyListeners(SWT.Selection, new Event());
button.notifyListeners(SWT.MouseDown, new Event());
}
});
}
@Override
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(
e -> System.out.println("Do next")));
I remember there was another question a few days ago regarding a long mouse click behaviour but I can't find it anymore. I put this code based on greg-449 solution to use timerExec method, after my failed attempts to use asyncExec in the UI thread. :)
button.addMouseListener(new MouseAdapter() {
boolean mouseDown;
@Override
public void mouseDown(MouseEvent e) {
mouseDown = true;
Display.getCurrent().timerExec(1000, () -> {
if (mouseDown) {
button.notifyListeners(SWT.Selection, new Event());
button.notifyListeners(SWT.MouseDown, new Event());
}
});
}
@Override
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});
button.addSelectionListener(SelectionListener.widgetSelectedAdapter(
e -> System.out.println("Do next")));
edited Nov 10 at 18:18
answered Nov 10 at 17:49
BogdanAdrian102
464
464
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232484%2fstarting-asyncexec-in-mouse-down-event-results-in-blocking-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Start by following the 'blocking' issue. Also, the variable must be
volatile
to ensure it is "seen" between the threads, should there be multiple threads.– user2864740
Nov 9 at 20:03
@user2864740 can you post some sample code with your proposal please? can't understand what do you want me to do
– NullPointerException
Nov 9 at 20:38
I don't have a proposal other than using
volatile boolean mouseDown
[my preference is to use explicit Threads, but I'm also "old school" like that] :) Hopefully someone will provide useful feedback possible UI-async interactions.– user2864740
Nov 9 at 20:47