为什么不在gxui中更改进度条的映射?

时间:2016-03-25 18:47:37

标签: go gxui

我想使用gxui中的进度条,但不会达到我的预期。 example可以正常工作,但改变它我没有成功。这是代码:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
    theme := flags.CreateTheme(driver)

    layout := theme.CreateLinearLayout()
    layout.SetHorizontalAlignment(gxui.AlignCenter)

    progressBar := theme.CreateProgressBar()
    progressBar.SetDesiredSize(math.Size{W: 480, H: 60})

    button := theme.CreateButton()
    button.SetText("Start")
    t0 := time.Now()
    button.OnClick(func(gxui.MouseEvent) {
        progressBar.SetTarget(100)
        N := 100

        for count := 0; count < N; count++ {
            resp, err := http.Get("http://example.com")
            if err != nil {
                log.Fatal(err)
            }
            defer resp.Body.Close()

            if count%10 == 0 {

                go func() {
                    driver.Call(func() {
                        fmt.Println("Tuk")
                        progressBar.SetProgress(count * 100 / N)
                    })
                }()
                fmt.Println(count)
                fmt.Println(ioutil.ReadAll(resp.Body))
                fmt.Printf("Elapsed time: %v\n", time.Since(t0))
            }
        }
        progressBar.SetProgress(50)
    })

    layout.AddChild(button)
    layout.AddChild(progressBar)

    window := theme.CreateWindow(500, 100, "Test")
    window.SetScale(flags.DefaultScaleFactor)
    window.AddChild(layout)
    window.OnClose(driver.Terminate)
}

func main() {
    gl.StartDriver(appMain)
}

由于我使用了goroutine,因此假设输出文本将交替,但所有goroutine仅在主线程之后执行打印。 我做错了什么以及如何解决它?

1 个答案:

答案 0 :(得分:0)

区别在于你的goroutine进入执行UI例程的队列,如documentation中所述:

  

//调用队列f在UI go-routine上运行,在f之前返回   可能已被召唤。

UI例程执行循环,因此无法同时处理更改 磁带ProgressBar。为了获得所需的结果,必须在单独的goroutine中运行处理功能。修改后的代码:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
    theme := flags.CreateTheme(driver)

    layout := theme.CreateLinearLayout()
    layout.SetHorizontalAlignment(gxui.AlignCenter)

    progressBar := theme.CreateProgressBar()
    progressBar.SetDesiredSize(math.Size{W: 480, H: 60})
    progressBar.SetTarget(100)
    button := theme.CreateButton()
    button.SetText("Start")
    t0 := time.Now()
    button.OnClick(func(gxui.MouseEvent) {
        go func() {
            N := 100
            for count := 0; count < N; count++ {
                resp, err := http.Get("http://example.com")
                if err != nil {
                    log.Fatal(err)
                }
                defer resp.Body.Close()

                driver.Call(func() {
                    progressBar.SetProgress(count * 100 / N)
                })

                fmt.Println(count)
                fmt.Println(ioutil.ReadAll(resp.Body))
                fmt.Printf("Elapsed time: %v\n", time.Since(t0))

            }
            progressBar.SetTarget(100)
        }()
    })

    layout.AddChild(button)
    layout.AddChild(progressBar)

    window := theme.CreateWindow(500, 100, "Test")
    window.SetScale(flags.DefaultScaleFactor)
    window.AddChild(layout)
    window.OnClose(driver.Terminate)

}

func main() {
    gl.StartDriver(appMain)
}