Wails: Quit app from within backend

Created on 22 Oct 2019  路  12Comments  路  Source: wailsapp/wails

It could very well be that I missed it somewhere but is there a way to programmatically exit the application I mean besides OS.exit() which is unclean?

Awaiting Feedback bug

Most helpful comment

In line 19 you return nil even tough no return value is declared.
Bildschirmfoto_2019-10-26_18-29-37

I use Ubuntu. Very weird that it does not invoke it on my system.

And for me the output (without returning an error in WailsShutdown) is:

Fermion - Debug Build
---------------------
INFO[0000] [App] Starting                               
INFO[0000] [WebView] Initialised                        
INFO[0000] [Events] Starting                            
INFO[0000] [Events] Listening                           
INFO[0000] [IPC] Starting                               
INFO[0000] [Bind] Starting                              
INFO[0000] [Bind] Binding Go Functions/Methods          
INFO[0000] [Bind] Bound Method: main.Runtime.Quit()     
INFO[0000] [Bind] Bound Method: main.Runtime.WailsShutdown() 
Bound Runtime
INFO[0000] [WebView] Running...                         
Goodbye!

All 12 comments

Hey @Pixdigit. Thanks for opening this. It's true, there isn't a specific runtime call to do this and I think it's a glaring omission. I consider this a bug and will add it this week :+1:

Technically, runtime.Window.Close() will exit from the app but there is no callback to allow you to shut down your resources, which is what I'm assuming you are wanting.

Hey there, I did a new release with a bunch of stuff around application shutdown. You can now hook into application shutdown using WailsShutdown. This will get called whenever you call runtime.Window.Close(), Kill the window or use ctrl-c (Windows is tricky on this front).

Example that I used for testing:

package main

import (
    "github.com/wailsapp/wails"
)

type Test struct {
    runtime *wails.Runtime
    log     *wails.CustomLogger
}

func NewTest() *Test {
    return &Test{}
}

func (t *Test) Kill() {
    t.runtime.Window.Close()
}

func (t *Test) WailsInit(runtime *wails.Runtime) error {
    t.runtime = runtime
    t.log = runtime.Log.New("Test Struct")
    return nil
}

func (t *Test) WailsShutdown() {
    t.log.Info("WailsShutdown() Called!")
}

Calling window.backend.Test.Kill() does exactly what you think it should.

Update using wails update and let me know how you get on.

Yes, that is what I mean. However in my setup (with v0.18.5) WailsShutdown isn't called. Besides in the api reference you return an error but do not declare a return type. So must WailsShutdown return an error? This is the code code I used for the testing. I also tried without returning an error.

Hi. Where in the reference does it state you need to return an error? I searched codebase plus docs. Also, what OS are you using?

Running your test app I get:

$ ./shutdown 
Bound Runtime
Shutting Down!

Running in debug mode and shutting down via js command window.backend.Runtime.Quit:

$ ./shutdown
Fermion - Debug Build
---------------------
INFO[0000] [App] Starting                               
INFO[0000] [WebView] Initialised                        
INFO[0000] [Events] Starting                            
INFO[0000] [IPC] Starting                               
INFO[0000] [Bind] Starting                              
INFO[0000] [Bind] Binding Go Functions/Methods          
INFO[0000] [Bind] Bound Method: main.Runtime.Quit()     
Bound Runtime
INFO[0000] [Events] Listening                           
INFO[0000] [WebView] Running...                         
Goodbye!
Shutting Down!

In line 19 you return nil even tough no return value is declared.
Bildschirmfoto_2019-10-26_18-29-37

I use Ubuntu. Very weird that it does not invoke it on my system.

And for me the output (without returning an error in WailsShutdown) is:

Fermion - Debug Build
---------------------
INFO[0000] [App] Starting                               
INFO[0000] [WebView] Initialised                        
INFO[0000] [Events] Starting                            
INFO[0000] [Events] Listening                           
INFO[0000] [IPC] Starting                               
INFO[0000] [Bind] Starting                              
INFO[0000] [Bind] Binding Go Functions/Methods          
INFO[0000] [Bind] Bound Method: main.Runtime.Quit()     
INFO[0000] [Bind] Bound Method: main.Runtime.WailsShutdown() 
Bound Runtime
INFO[0000] [WebView] Running...                         
Goodbye!

@Pixdigit I think the return nil it's probably a typo.

Thanks @Pixdigit. Will fix the docs!

Also, Please test against the latest pre release wails update -pre for the Shutdown issue.

I made an empty test project with the code shared in pastebin. In that case everything works as intended.
Turns out I forgot to wails migrate. To mitigate this issue maybe execute wails migrate automatically after wails update since it modifies the project only with consent.

Ah that's great news. wails migrate is really new and I think I'd rather that be a manual process as it's a one-off operation. Really pleased it's working for you and thanks for opening this issue. It's something we really needed to address and the project as a whole is much better for it :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kelus picture kelus  路  6Comments

best2000 picture best2000  路  7Comments

VladislavSournine picture VladislavSournine  路  5Comments

bh90210 picture bh90210  路  4Comments

Lyimmi picture Lyimmi  路  4Comments