Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I run the thread example and try to move the window, it doesn't respond and the program is dead. #424

Open
HenryZhouHB opened this issue Mar 22, 2022 · 2 comments

Comments

@HenryZhouHB
Copy link

No description provided.

@HenryZhouHB HenryZhouHB changed the title When I run the thread example and try to move the window, it doesn't respond. When I run the thread example and try to move the window, it doesn't respond and the program is dead. Mar 22, 2022
@mattn
Copy link
Owner

mattn commented Mar 22, 2022

Please try to add runtime.LockOSThread() on the top of main func.

@HenryZhouHB
Copy link
Author

HenryZhouHB commented Mar 22, 2022

Thank you for your advice.I follow it, but it can not solve this issue.Any other advice? My code is as below:

package main

import (
	"runtime"
	"strconv"
	"time"

	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/glib"
	"github.com/mattn/go-gtk/gtk"
)

func main() {
	runtime.LockOSThread()
	runtime.GOMAXPROCS(10)
	glib.ThreadInit(nil)
	gdk.ThreadsInit()
	gdk.ThreadsEnter()
	gtk.Init(nil)
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(false, 1)

	label1 := gtk.NewLabel("")
	vbox.Add(label1)
	label2 := gtk.NewLabel("")
	vbox.Add(label2)

	window.Add(vbox)

	window.SetSizeRequest(800, 600)
	window.ShowAll()
	time.Sleep(1000 * 1000 * 100)
	go (func() {
		for i := 0; i < 30000000; i++ {
			gdk.ThreadsEnter()
			label1.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	go (func() {
		for i := 30000000; i >= 0; i-- {
			gdk.ThreadsEnter()
			label2.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	gtk.Main()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants