2016-03-25 16 views
1

我想使用gxui的进度条,但得不到我的预期。 example按原样工作,但改变它我没有成功。下面是代码:为什么不改变gxui中的进度条的映射?

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只有主线程后进行打印。 我在做什么错,如何解决?

+0

见http://stackoverflow.com/questions/10095751/understanding-goroutines – user1431317

+0

@ user1431317,我不知道你理解错我的问题 –

+0

啊,对不起,我没看到http.Get在那里。这可能导致主循环产生。 – user1431317

回答

0

不同的是,你够程进入队列执行UI例程,如写在documentation

//呼叫队列F进行的UI够程运行,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) 
}