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;
}
});









share|improve this question
























  • 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

















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;
}
});









share|improve this question
























  • 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















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;
}
});









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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














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.






share|improve this answer





















  • 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


















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")));





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer





















    • 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















    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.






    share|improve this answer





















    • 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













    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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")));





    share|improve this answer



























      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")));





      share|improve this answer

























        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")));





        share|improve this answer














        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")));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 18:18

























        answered Nov 10 at 17:49









        BogdanAdrian102

        464




        464






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues