andlabs / ui

Platform-native GUI library for Go.
Other
8.33k stars 652 forks source link

wait without blocking main thread #374

Closed jeromelesaux closed 3 years ago

jeromelesaux commented 4 years ago

In a Onclick button function I do a treatment in background (set a progress bar) like this :

var onError = false
func myOnClick(b *ui.Button) {
 b.Disable()
onError = false 
 go dosometing()
if onError {
        ui.MsgBoxError(Mainwin, "Download Error !", "my error")
 }
 b.Enable()
}
func checkProgress(i int) {
    ui.QueueMain(func() {
        dowloadProgress.SetValue(i)
    })
}

func do something() {
 for i:=0; i<1000; i++ {
  checkProgress(i)
 }
 onError = true
}

How can I wait for the end of the dosomething function without blocking main thread ?

aggyomfg commented 4 years ago

Try something like this, run it in gorutine

func checkProgress() {
    ticker := time.NewTicker(time.Second / 5).C
    for {
        select {
        case <-ticker:
            ui.QueueMain(func() {
                mainwin.SetTitle(comp.UIState.Status)
            })
        }
    }
}
jeromelesaux commented 4 years ago

thanks I try this hint.

jeromelesaux commented 3 years ago

works fine.