Search code examples
c#cocoamonomonomac

Refresh UI on ApplicationShouldTerminate


I have a trouble, when i try to update the text of a button at time to exit from the application.

The ApplicationShouldTerminate method is like this:

        public override NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)
    {
        this.mainWindowController.EndApp();
        return NSApplicationTerminateReply.Now;
    }

And the EndApp() method is like this:

        public void EndApp ()
    {
        this.testButton.Title="Closing App";
        //long time process
        for (int i = 0; i < 1000000; i++) {
            int a=1;
        }
    }

How can i get that the application refresh the UI Controls??


Solution

  • You can add an Application.DoEvents() after the title change or in the for loop depending on if you have other actions going on. This forces the application to handle events in the queue, which in this case would be a text change for your title.

    public void EndApp ()
        {
            this.testButton.Title="Closing App";
            Application.DoEvents();
            //long time process
            for (int i = 0; i < 1000000; i++) {
                int a=1;
                //or if you're processing stuff
                Application.DoEvents();
            }
        }