giu icon indicating copy to clipboard operation
giu copied to clipboard

How to add a check for `syscall.SIGINT`?

Open cydside opened this issue 4 years ago • 5 comments

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 avatar Mar 19 '21 09:03 cydside

@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?

AllenDang avatar Mar 23 '21 13:03 AllenDang

Yes, the scenario is to call closing/cleaning functions in case of syscall.SIGINT crops up.

cydside avatar Mar 23 '21 13:03 cydside

@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
})

HACKERALERT avatar Aug 20 '21 13:08 HACKERALERT

@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")
}

gucio321 avatar Oct 07 '21 16:10 gucio321

@cydside any updates here?

gucio321 avatar Jul 22 '22 05:07 gucio321

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!

gucio321 avatar May 08 '23 13:05 gucio321