2016-03-25 47 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) 
}