lxn/walk goroutine blocks

238 views
Skip to first unread message

rbi...@cox.net

unread,
Oct 10, 2016, 4:46:11 PM10/10/16
to golang-nuts
Hi Guys,
I’m building a GUI for my robot.  I’m using Windows, so found lxn/walk.   Working from the examples, I built a GUI which did all that I want right now.  I developed it in the main() function, just like the examples; but for the actual robot, I want it to run in its own goroutine with the code in a separate package.  Well, I can’t make that work.  I can’t even get it to work as a goroutine in main().
The minimal example code below demonstrates the problem.  The main() function has two ways to start the GUI.  If I run the “Gui()” version.  The GUI appears on the screen as expected and the controls function.  However, the subsequent 5 second Sleep and Println() don’t begin to run until I close the GUI window by clicking the X.  If I run the “go Gui()” version,  The GUI does not appear on the screen, but the code does continue and prints after the 5 second sleep.
I expect that it is possible to do what I want (i.e.  "go Gui” with continuation to the sleep and Println).  I am pretty much a newbie, but I have written a number of relatively complex goroutines that don’t have this problem.
I’m running on Windows 10 (x64) with go 1.6.2 on LiteIDE X29 and a very recent walk download.
I’m hoping someone can point out my possible naïve error, or maybe a link to some code that does what I want.
Thanks,
Alex

package main

import (
    "fmt"
    "time"
        . "github.com/lxn/walk/declarative"
)

func main() {
    //Gui() //  draws operative GUI window, but blocks from continuing to Sleep and Println
    go Gui() // doesn't draw GUI window. Continues and does Println after 5 seconds

    time.Sleep(time.Millisecond * 5000)
    fmt.Println("testing")
}

func Gui() {
    MainWindow{
        Title:   "SCREAMO",
        MinSize: Size{600, 400},
    }.Run()
}

Tamás Gulácsi

unread,
Oct 11, 2016, 3:51:17 AM10/11/16
to golang-nuts
I suspect that the main thread has to communicate with the gui libs. This is a common restrictio.
Live with it: do the other stuff in a goroutine.

Pietro Gagliardi

unread,
Oct 11, 2016, 7:49:34 AM10/11/16
to golang-nuts


Begin forwarded message:

From: Pietro Gagliardi <and...@lostsig.net>
Subject: Re: [go-nuts] lxn/walk goroutine blocks
Date: October 11, 2016 at 7:48:04 AM EDT
To: Tamás Gulácsi <tgula...@gmail.com>

That would mean lxn/walk is doing initialization on the init() thread, because Windows does not care which thread you use for GUI stuff, and in fact lets you do GUI stuff on multiple threads at once. (You just can't *cross* threads; if you call CreateWindow() on a thread, you have to use that window from that same thread, and any other thread can only SendMessage() or PostMessage() to it.)
On Oct 11, 2016, at 3:51 AM, Tamás Gulácsi <tgula...@gmail.com> wrote:

I suspect that the main thread has to communicate with the gui libs. This is a common restrictio.
Live with it: do the other stuff in a goroutine.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages