diff --git a/init.go b/init.go index d8203e2..a6a4882 100644 --- a/init.go +++ b/init.go @@ -24,6 +24,7 @@ func Go() error { Ready <- struct{}{} close(Ready) ui() + return nil } // Ready is pulsed when Go() is ready to begin accepting requests to the safe methods. diff --git a/uitask_windows.go b/uitask_windows.go index 1075b5b..1eb44f4 100644 --- a/uitask_windows.go +++ b/uitask_windows.go @@ -46,10 +46,7 @@ var ( _postMessage = user32.NewProc("PostMessageW") ) -func ui(main func()) error { - runtime.LockOSThread() - - uitask = make(chan interface{}) +func uiinit() error { err := doWindowsInit() if err != nil { return fmt.Errorf("error doing general Windows initialization: %v", err) @@ -60,33 +57,38 @@ func ui(main func()) error { return fmt.Errorf("error making invisible window for handling events: %v", err) } + // do this only on success just to be safe + uitask = make(chan interface{}) + return nil +} + +func ui() { go func() { - for m := range uitask { - r1, _, err := _postMessage.Call( - uintptr(hwnd), - msgRequested, - uintptr(0), + for { + select { + case m := <-uitask: + r1, _, err := _postMessage.Call( + uintptr(hwnd), + msgRequested, + uintptr(0), uintptr(unsafe.Pointer(&m))) - if r1 == 0 { // failure - panic("error sending message to message loop to call function: " + err.Error()) + if r1 == 0 { // failure + panic("error sending message to message loop to call function: " + err.Error()) + } + case <-Stop: + r1, _, err := _postMessage.Call( + uintptr(hwnd), + msgQuit, + uintptr(0), + uintptr(0)) + if r1 == 0 { // failure + panic("error sending quit message to message loop: " + err.Error()) + } } } }() - go func() { - main() - r1, _, err := _postMessage.Call( - uintptr(hwnd), - msgQuit, - uintptr(0), - uintptr(0)) - if r1 == 0 { // failure - panic("error sending quit message to message loop: " + err.Error()) - } - }() - msgloop() - return nil } var (