cogentcore / core

A free and open source framework for building powerful, fast, and elegant 2D and 3D apps that run on macOS, Windows, Linux, iOS, Android, and the Web with a single pure Go codebase, allowing you to Code Once, Run Everywhere.
http://cogentcore.org/core
BSD 3-Clause "New" or "Revised" License
1.31k stars 71 forks source link

The program is not responding #924

Closed qq1792569310 closed 4 months ago

qq1792569310 commented 4 months ago

Describe the bug

When there is no waiting, execute two or more consecutive *gi. Body.RunMainWindow() function, there is a high probability that the program will not respond

How to reproduce

My file structure: async --async.go main.go

async.go is from the async.go file of the example, and main.go is from the demo document.

At the beginning of main.go, I executed async.go with a goroutine and it worked fine the first time, but when I started core run a second time, the program didn't respond

async.go:Just changed the package name and changed the function name 'main' to 'Async'

package async

import (
    "time"

    "cogentcore.org/core/events"
    "cogentcore.org/core/gi"
    "cogentcore.org/core/giv"
    "cogentcore.org/core/icons"
)

type tableStruct struct {
    Icon       icons.Icon
    IntField   int
    FloatField float32
    StrField   string
    File gi.Filename
}

const rows = 100000

func Async() {
    fmt.Println("Async开始")
    table := make([]*tableStruct, 0, rows)
    b := gi.NewBody("Async Updating")
    tv := giv.NewTableView(b)
    tv.SetReadOnly(true)
    tv.SetSlice(&table)
    b.OnShow(func(e events.Event) {
        go func() { 
            for i := 0; i < rows; i++ {
                updt := b.UpdateStartAsync()
                table = append(table, &tableStruct{IntField: i, FloatField: float32(i) / 10.0})
                tv.UpdateWidgets()
                if len(table) > 0 {
                    tv.ScrollToIdx(len(table) - 1)
                }
                b.UpdateEndAsyncLayout(updt)
                time.Sleep(1 * time.Millisecond)
                // time.Sleep(1 * time.Second)
            }
        }()
    })
        //time.Sleep(5 * time.Second)
    b.RunMainWindow()
}

main.go: It's just that at the very beginning I used 'goroutine' to call 'async. Async()'

package main

import (
    "embed"
    "fmt"
    "io/fs"
    "myapp/async"

    "cogentcore.org/core/events"
    "cogentcore.org/core/gi"
    "cogentcore.org/core/grr"
    "cogentcore.org/core/icons"
)

//go:embed icons/*.svg
var myIcons embed.FS

func main() {

    go async.Async()//here

    fmt.Println("")
    b := gi.NewBody("hello")
    gi.NewButton(b).SetText("hello , world")
    gi.NewButton(b).SetText("Send").SetIcon(icons.Send).OnClick(func(e events.Event) {
        gi.MessageSnackbar(b, "Message sent")
    })
    gi.NewButton(b).SetText("Click me").OnClick(func(e events.Event) {
        gi.MessageSnackbar(b, "Button clicked")
    })
    gi.NewButton(b).SetText("Click me").OnClick(func(e events.Event) {
        gi.MessageSnackbar(b, fmt.Sprint("Button clicked at", e.Pos()))
        e.SetHandled()
    })
    gi.NewButton(b).SetText("Click me").SetIcon(icons.Add)
    sw := gi.NewSwitch(b).SetText("Switch me!")
    gi.MessageSnackbar(b, sw.Text)
    icons.AddFS(grr.Log1(fs.Sub(myIcons, "icons")))
    b.AddCloseDialog(func(d *gi.Body) bool {
        d.AddTitle("Are you sure?").AddText("Are you sure you want to close the Cogent Core Demo?")
        d.AddBottomBar(func(pw gi.Widget) {
            d.AddOk(pw).SetText("Close").OnClick(func(e events.Event) {
                b.Scene.Close()
            })
        })
        return true
    })
    b.RunMainWindow()
}

The solution might be in the second gi.Body. RunMainWindow() before execution, wait a while? async.go: When I waited 5 seconds in async.go, the bug didn't appear

time.Sleep(5 * time.Second)
b.RunMainWindow()

Example code

No response

Relevant output

No response

Platform

Windows

qq1792569310 commented 4 months ago

One more question, right now after I run core run, there are two windows, a hello window created by main.go and an Async Updating window created by async.go.

When I am in the Async Updating window and click window>Quit, the Async Updating window gets stuck (not unresponsive) and cannot exit, the hello window works fine still at this point, then I click on the close button (the x in the upper right corner) of the hello window, and the hello window gets stuck as well.

At this point there is no way to close the program through the window, I have to use the task manager to end the process.

R6S5M_8S30UFA@1N}JX9ZJ

kkoreilly commented 4 months ago

The documentation for RunMainWindow states that:

RunMainWindow creates a new main window from the body, runs it, starts the app's main loop, and waits for all windows to close. It should typically be called once by every app at the end of their main function. It can not be called more than once for one app. For more specific configuration and for secondary windows, see [Body.NewWindow].

You cannot call RunMainWindow twice; instead, you should just call b.NewWindow().Run() for secondary windows.

ddkwork commented 4 months ago

The documentation for RunMainWindow states that:

RunMainWindow creates a new main window from the body, runs it, starts the app's main loop, and waits for all windows to close. It should typically be called once by every app at the end of their main function. It can not be called more than once for one app. For more specific configuration and for secondary windows, see [Body.NewWindow].

You cannot call RunMainWindow twice; instead, you should just call b.NewWindow().Run() for secondary windows.

Hello teacher, aren't you sleeping at this time? I guess it's about three o'clock in the morning, right?

kkoreilly commented 4 months ago

No, it is 11:25 PM. I am going to sleep now.

ddkwork commented 4 months ago

Okay

---Original--- From: @.> Date: Sat, Mar 2, 2024 15:26 PM To: @.>; Cc: @.**@.>; Subject: Re: [cogentcore/core] The program is not responding (Issue #924)

No, it is 11:25 PM. I am going to sleep now.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>