Stop NSWindow from receiving input events temporarily











up vote
0
down vote

favorite












I want to prevent an NSWindow from receiving (or handling) inputs events temporarily. As if a sheet is displayed or the run loop is busy executing code. How can I do that?



Why?



Currently when the user executes a 'command' in my application it's handled on the main thread. This of course causes the window to become unresponsive for the duration of the command. This is fine most of the time as the commands normally take 1 - 2 seconds to execute.



Sometimes though the command can take a very long time (60 seconds or more). To give the user an option to cancel the command I want to execute it in the background and if the command does not finish in about 2 seconds I want to put up a sheet with some information and a cancel button.



I don't want to put up the sheet immediately, as that would cause very annoying appear/disappear animations most of the time, but I need to block the GUI for those first 2 seconds.










share|improve this question






















  • You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
    – Dmitry Serov
    Nov 12 at 9:30










  • After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
    – Remco Poelstra
    Nov 12 at 11:37










  • If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
    – Dmitry Serov
    Nov 12 at 12:52










  • Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
    – Remco Poelstra
    Nov 12 at 12:55










  • Why do you need to block the UI, then?
    – Dmitry Serov
    Nov 12 at 13:03















up vote
0
down vote

favorite












I want to prevent an NSWindow from receiving (or handling) inputs events temporarily. As if a sheet is displayed or the run loop is busy executing code. How can I do that?



Why?



Currently when the user executes a 'command' in my application it's handled on the main thread. This of course causes the window to become unresponsive for the duration of the command. This is fine most of the time as the commands normally take 1 - 2 seconds to execute.



Sometimes though the command can take a very long time (60 seconds or more). To give the user an option to cancel the command I want to execute it in the background and if the command does not finish in about 2 seconds I want to put up a sheet with some information and a cancel button.



I don't want to put up the sheet immediately, as that would cause very annoying appear/disappear animations most of the time, but I need to block the GUI for those first 2 seconds.










share|improve this question






















  • You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
    – Dmitry Serov
    Nov 12 at 9:30










  • After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
    – Remco Poelstra
    Nov 12 at 11:37










  • If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
    – Dmitry Serov
    Nov 12 at 12:52










  • Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
    – Remco Poelstra
    Nov 12 at 12:55










  • Why do you need to block the UI, then?
    – Dmitry Serov
    Nov 12 at 13:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to prevent an NSWindow from receiving (or handling) inputs events temporarily. As if a sheet is displayed or the run loop is busy executing code. How can I do that?



Why?



Currently when the user executes a 'command' in my application it's handled on the main thread. This of course causes the window to become unresponsive for the duration of the command. This is fine most of the time as the commands normally take 1 - 2 seconds to execute.



Sometimes though the command can take a very long time (60 seconds or more). To give the user an option to cancel the command I want to execute it in the background and if the command does not finish in about 2 seconds I want to put up a sheet with some information and a cancel button.



I don't want to put up the sheet immediately, as that would cause very annoying appear/disappear animations most of the time, but I need to block the GUI for those first 2 seconds.










share|improve this question













I want to prevent an NSWindow from receiving (or handling) inputs events temporarily. As if a sheet is displayed or the run loop is busy executing code. How can I do that?



Why?



Currently when the user executes a 'command' in my application it's handled on the main thread. This of course causes the window to become unresponsive for the duration of the command. This is fine most of the time as the commands normally take 1 - 2 seconds to execute.



Sometimes though the command can take a very long time (60 seconds or more). To give the user an option to cancel the command I want to execute it in the background and if the command does not finish in about 2 seconds I want to put up a sheet with some information and a cancel button.



I don't want to put up the sheet immediately, as that would cause very annoying appear/disappear animations most of the time, but I need to block the GUI for those first 2 seconds.







macos






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 12:09









Remco Poelstra

17711




17711












  • You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
    – Dmitry Serov
    Nov 12 at 9:30










  • After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
    – Remco Poelstra
    Nov 12 at 11:37










  • If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
    – Dmitry Serov
    Nov 12 at 12:52










  • Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
    – Remco Poelstra
    Nov 12 at 12:55










  • Why do you need to block the UI, then?
    – Dmitry Serov
    Nov 12 at 13:03


















  • You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
    – Dmitry Serov
    Nov 12 at 9:30










  • After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
    – Remco Poelstra
    Nov 12 at 11:37










  • If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
    – Dmitry Serov
    Nov 12 at 12:52










  • Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
    – Remco Poelstra
    Nov 12 at 12:55










  • Why do you need to block the UI, then?
    – Dmitry Serov
    Nov 12 at 13:03
















You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
– Dmitry Serov
Nov 12 at 9:30




You say 'I want to execute it in the background', but you also want to 'block the GUI for those first 2 seconds'. This seems like a contradiction to me.
– Dmitry Serov
Nov 12 at 9:30












After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
– Remco Poelstra
Nov 12 at 11:37




After the first two seconds I want to put up a sheet with a cancel button. I need the main run loop for that, so the command needs to execute in the background. The sheet will then take care of blocking the main window.
– Remco Poelstra
Nov 12 at 11:37












If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
– Dmitry Serov
Nov 12 at 12:52




If the GUI is blocked for the first 2 seconds, why don't you just display the sheet straight away with the cancel button disabled? Enable the button once 2 seconds are up, and, as a bonus, there will be no beach-balling.
– Dmitry Serov
Nov 12 at 12:52












Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
– Remco Poelstra
Nov 12 at 12:55




Because most of the time the command runs very short and I want to prevent a lot of half appearing and directly disappearing sheets.
– Remco Poelstra
Nov 12 at 12:55












Why do you need to block the UI, then?
– Dmitry Serov
Nov 12 at 13:03




Why do you need to block the UI, then?
– Dmitry Serov
Nov 12 at 13:03












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can achieve this by overriding sendEvent(_ event: NSEvent) in your NSWindow subclass. Use a flag to let the window know that it should not handle events temporarily:



override func sendEvent(_ event: NSEvent) {
guard shouldSendEvents else { return }
super.sendEvent(event)
}





share|improve this answer




























    up vote
    0
    down vote













    I'm not sure this UI design is good, but if you must… you can use a semaphore. Initialize it with a count of 0. The background thread will signal it when it completes. The main thread can, after spawning the background task, wait on it with a timeout of 2 seconds. If the background task finishes first, the main thread will resume at that time. If the timeout expires, the main thread will resume and can show the sheet with the Cancel button.



    By the way, if the background task takes just over 2 seconds, you will still show a sheet and then immediately dismiss it.






    share|improve this answer





















    • I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
      – Remco Poelstra
      Nov 16 at 9:40










    • As for the GUI design, I'm open for suggestions :)
      – Remco Poelstra
      Nov 16 at 9:43











    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%2f53248592%2fstop-nswindow-from-receiving-input-events-temporarily%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
    0
    down vote



    accepted










    You can achieve this by overriding sendEvent(_ event: NSEvent) in your NSWindow subclass. Use a flag to let the window know that it should not handle events temporarily:



    override func sendEvent(_ event: NSEvent) {
    guard shouldSendEvents else { return }
    super.sendEvent(event)
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      You can achieve this by overriding sendEvent(_ event: NSEvent) in your NSWindow subclass. Use a flag to let the window know that it should not handle events temporarily:



      override func sendEvent(_ event: NSEvent) {
      guard shouldSendEvents else { return }
      super.sendEvent(event)
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You can achieve this by overriding sendEvent(_ event: NSEvent) in your NSWindow subclass. Use a flag to let the window know that it should not handle events temporarily:



        override func sendEvent(_ event: NSEvent) {
        guard shouldSendEvents else { return }
        super.sendEvent(event)
        }





        share|improve this answer












        You can achieve this by overriding sendEvent(_ event: NSEvent) in your NSWindow subclass. Use a flag to let the window know that it should not handle events temporarily:



        override func sendEvent(_ event: NSEvent) {
        guard shouldSendEvents else { return }
        super.sendEvent(event)
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 17:10









        Dmitry Serov

        618720




        618720
























            up vote
            0
            down vote













            I'm not sure this UI design is good, but if you must… you can use a semaphore. Initialize it with a count of 0. The background thread will signal it when it completes. The main thread can, after spawning the background task, wait on it with a timeout of 2 seconds. If the background task finishes first, the main thread will resume at that time. If the timeout expires, the main thread will resume and can show the sheet with the Cancel button.



            By the way, if the background task takes just over 2 seconds, you will still show a sheet and then immediately dismiss it.






            share|improve this answer





















            • I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
              – Remco Poelstra
              Nov 16 at 9:40










            • As for the GUI design, I'm open for suggestions :)
              – Remco Poelstra
              Nov 16 at 9:43















            up vote
            0
            down vote













            I'm not sure this UI design is good, but if you must… you can use a semaphore. Initialize it with a count of 0. The background thread will signal it when it completes. The main thread can, after spawning the background task, wait on it with a timeout of 2 seconds. If the background task finishes first, the main thread will resume at that time. If the timeout expires, the main thread will resume and can show the sheet with the Cancel button.



            By the way, if the background task takes just over 2 seconds, you will still show a sheet and then immediately dismiss it.






            share|improve this answer





















            • I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
              – Remco Poelstra
              Nov 16 at 9:40










            • As for the GUI design, I'm open for suggestions :)
              – Remco Poelstra
              Nov 16 at 9:43













            up vote
            0
            down vote










            up vote
            0
            down vote









            I'm not sure this UI design is good, but if you must… you can use a semaphore. Initialize it with a count of 0. The background thread will signal it when it completes. The main thread can, after spawning the background task, wait on it with a timeout of 2 seconds. If the background task finishes first, the main thread will resume at that time. If the timeout expires, the main thread will resume and can show the sheet with the Cancel button.



            By the way, if the background task takes just over 2 seconds, you will still show a sheet and then immediately dismiss it.






            share|improve this answer












            I'm not sure this UI design is good, but if you must… you can use a semaphore. Initialize it with a count of 0. The background thread will signal it when it completes. The main thread can, after spawning the background task, wait on it with a timeout of 2 seconds. If the background task finishes first, the main thread will resume at that time. If the timeout expires, the main thread will resume and can show the sheet with the Cancel button.



            By the way, if the background task takes just over 2 seconds, you will still show a sheet and then immediately dismiss it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 17:29









            Ken Thomases

            68.8k668105




            68.8k668105












            • I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
              – Remco Poelstra
              Nov 16 at 9:40










            • As for the GUI design, I'm open for suggestions :)
              – Remco Poelstra
              Nov 16 at 9:43


















            • I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
              – Remco Poelstra
              Nov 16 at 9:40










            • As for the GUI design, I'm open for suggestions :)
              – Remco Poelstra
              Nov 16 at 9:43
















            I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
            – Remco Poelstra
            Nov 16 at 9:40




            I realise that the sheet is sometimes still shown very short, but only in 0.1% of the cases, instead of 98%. I think this is acceptable.
            – Remco Poelstra
            Nov 16 at 9:40












            As for the GUI design, I'm open for suggestions :)
            – Remco Poelstra
            Nov 16 at 9:43




            As for the GUI design, I'm open for suggestions :)
            – Remco Poelstra
            Nov 16 at 9:43


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248592%2fstop-nswindow-from-receiving-input-events-temporarily%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

            Full-time equivalent

            Bicuculline

            さくらももこ