How to add a check for `syscall.SIGINT`?
Hi, I'm wondering if there's a way to prevent the application to be closed by a SIGINT call, here is my attempt:
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/AllenDang/giu"
)
var (
counter int
)
func refresh() {
ticker := time.NewTicker(time.Second * 3)
for {
counter = rand.Intn(100)
giu.Update()
<-ticker.C
}
}
func loop() {
giu.SingleWindow("Update").Layout(
giu.Label("Below number is updated by a goroutine"),
giu.Label(fmt.Sprintf("%d", counter)),
)
}
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
fmt.Println()
fmt.Println(sig)
done <- true
}()
wnd := giu.NewMasterWindow("Update", 400, 200, giu.MasterWindowFlagsNotResizable, nil)
go refresh()
wnd.Run(loop) // go wnd.Run(loop) doesn't show components in the window
fmt.Println("awaiting signal") // never run until window close
<-done
fmt.Println("exiting")
}
Thanks in advance for your help
@cydside I think this should be an discussion about how to avoid termination triggered by syscall.SIGINT rather than an issue. May I ask what's the usage scenario?
Yes, the scenario is to call closing/cleaning functions in case of syscall.SIGINT crops up.
@cydside Not sure if this satisfies your needs, but you can set a callback when the user tries to close a window. That's how I do cleanup in my program.
window.SetCloseCallback(func() bool{
if working{
return false
}
cleanup()
return true
})
@cydside this code should do what are you going to:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/AllenDang/giu"
)
func loop() {
giu.SingleWindow().Layout(
giu.Label("label"),
)
}
func main() {
// set up a chanel to recive os signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// set up master window
wnd := giu.NewMasterWindow("wnd", 640, 480, 0)
// set close callback for standard situations
wnd.SetCloseCallback(func() bool {
onClose()
return true
})
// catch and handle terminated signals
go func() {
<-sigs
// if you want to close master window
wnd.Close()
onClose()
}()
wnd.Run(loop)
}
// put everything what you want to do when close event happens
func onClose() {
fmt.Println("closing")
}
@cydside any updates here?
due to a long inactivity (and full code demo posted above) I think it could be closed for now. If you had any questions, feel free to ping me!