Skip to content

QueueUpdate(Draw) deadlocks in goroutine #690

@craftyguy

Description

@craftyguy

I'm trying to update the text in a textview, and wrapping it in a QueueUpdate (or QueueUpdateDraw), per the wiki page on concurrency, deadlocks 100% of the time:

package main

import (
        "fmt"
        "time"

        "github.com/rivo/tview"
)

var (
        tv  *tview.TextView    = tview.NewTextView()
        app *tview.Application = tview.NewApplication()
)

func update() {
        var i int
        for {
                app.QueueUpdateDraw(func() {
                        tv.SetText(fmt.Sprintf("%d", i))
                })

                time.Sleep(1 * time.Second)
                i++
        }
}

func main() {
        go update()

        if err := tview.NewApplication().SetRoot(tv, true).EnableMouse(true).Run(); err != nil {
                panic(err)
        }
}

The wiki says I should call Application.Draw in the "changed" handler for a textview, however this does not result in the screen being updated with any changes when using SetText:

...(snipped unchanged code)...

func update() {
        var i int
        for {
                tv.SetText(fmt.Sprintf("%d", i))

                time.Sleep(400 * time.Millisecond)
                i++
        }
}

func changed() {
        app.Draw()
}

func main() {
        tv.SetChangedFunc(changed)
        go update()

...(snipped unchanged code)...

What's the correct way to update a textview from a goroutine?

I'm using Go 1.17.4 on Alpine Linux (amd64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions