第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么不改變 gxui 中進度條的映射?

為什么不改變 gxui 中進度條的映射?

Go
白衣非少年 2021-12-27 17:02:25
我想使用gxui的進度條,但沒有達到我的預期。該示例正常工作,但更改它我沒有成功。這是代碼:package mainimport (    "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 僅在主線程之后執(zhí)行打印。我做錯了什么以及如何解決?
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

不同之處在于您的 goroutine 進入執(zhí)行 UI 例程的隊列,如文檔中所述:


// 調用隊列 f 在 UI go-routine 上運行,在 f 可能被調用之前返回。


UI-routine 執(zhí)行一個循環(huán),所以不能同時處理改變磁帶的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)

}


查看完整回答
反對 回復 2021-12-27
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號