Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Application.Quit()

Discussion in 'Scripting' started by gambr, Sep 5, 2019.

  1. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    Hi All,
    what really happens when I call "Application.Quit()"? I mean, if a Unity update process is running, is it interrupted or completed for every MonoBehaviour object?
    Can "Application.Quit()" be called from a different thread or from a Timer?

    Regards,
    Gianni
     
  2. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    When you build a project, and call Application.Quit(); it will automatically quit.
     
  3. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    That's OK, but I asked more details.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    I never tested this, but i'd imagine that Application.Quit() can only be called from the main thread, as it is part of the Unity API. Thus, since code is executed sequentially, with the exception of multithreading, the application will quit as soon as Application.Quit() is called somewhere in the program. I'd imagine this would be the same case if it was possible to be used multithreaded tho. Depends a bit on the implementation, since it may also toggle a bool or something that is checked before each update loop.
    Why are you asking? Just some curiosity, or maybe a problem you need help with?
     
    Last edited: Sep 5, 2019
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I'm also guessing it can only be called from the main thread. It would be fairly easy to test whether or not other Update methods are still called for that frame.

    I've made a MonoBehaviour singleton before that makes calls main thread safe. It stores calls made and calls them in the next update cycle. That would also work for Application.Quit if needed.

    Finally, note that there is an OnApplicationQuit in MonoBehaviour. So if you need to do something just as the application is being quit, you can do it there instead of in Update. If this action takes a while, you can even start up a new thread at this point. (As long as you don't need any main thread calls anymore.) Just make sure this thread ends at some point, so the application will actually quit.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,346
    There's some info in the docs about what happens.

    If you look at the flowchart on the same page, it does look like the game is quit at the end of the frame when it was called, rather than immidiately.
     
    Ryiah and Yoreki like this.
  7. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    Thanks Yoreki,
    I have a C++ application (the Controller) and an application built with Unity (the Visual). The Controller can run the Visual and also quit the Visual. Controller and Visual communicate via Apache Thrift. The Visual runs in exclusive mode (for performance reasons). When the Controller asks the Visual to quit, then I implemented a way (I have to skip the details here) to add this event to the "update()" implementation of my Main.cs (a MonoBehaviour) so that in the next update() call Application.Quit() is executed. This works fine but in one case: Controller and Visual run on the same PC (Windows). In fact since the Visual runs in exclusive mode it is iconized as soon as the focus goes to the Controller application. When the Viewer is iconized no update() is called, so Application.Quit() is never called.

    That's why I would like to know if I can call Application.Quit() from another thread or timer: to be independent on update() calls.

    I need a way to quit the Unity application (the Visual) that works in every scenario, even if the Unity application is iconized.
     
  8. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    Yes, I have to write some strings to a log file since Application.Quit() does not wotk inside the Unity Editor. Not such fun ... :-(

    If I understood correctly, I did something similar. But since I don't want to use singletons because of code testability, I used a Command Pattern instead and append commands to process on every update to my Main MonoBehaviour-derived class. One of those command is ApplicationQuitCommand. There I call Application.Quit().

    I already implemented my application cleanup in OnApplicationQuit, thanks ;-)
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Not entirely sure about your setup, but is "run in background"(or whatever it's called) enabled?
     
  10. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    Thanks for the interesting link. So, when an Application.Quit() is called, for example in a update() implementation of a MonoBehaviour, the flow goes on till End of frame then a sort of flag tells Unity to handle the Application quit and OnApplicationQuit is called for every MonoBehaviour. Then application exits. Is that right?
     
  11. gambr

    gambr

    Joined:
    Oct 13, 2017
    Posts:
    109
    You may be right, but is "Application.runInBackground = true" compatible with FullScreenMode.ExclusiveFullScreen?
     
  12. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    A multiscreen setup can have sn unfocused fullscreen window, not sure if it applies to you.