Closed abenz1267 closed 8 months ago
these seems to happen if the surrounding function exists, adding a waitgroup and waiting for the routine to finish fixes it. am i being dumb?
Your widget code must execute in the main UI thread. glib.IdleAdd can be used to run a function on the main thread.
So instead of
go func() {
list.SetVisible(false)
}()
do this
go func() {
// some code leads to a decision to change visibility of list
...
glib.IdleAdd(func() {
list.SetVisible(false)
})
}()
The gotk4-examples repo has an example of using glib.IdleAdd
.
https://github.com/diamondburned/gotk4-examples/blob/66e0fb4a758fc5ad75073319f67d5efc8d3630b4/gtk4/goroutines/main.go#L50
Aaahhhh.... thanks you a ton.
A bit more information about gtk and threads, and the need to use glib.IdleAdd
.
https://docs.gtk.org/gtk4/question_index.html
How do I use GTK with threads?
GTK requires that all GTK API calls are made from the same thread in which the GtkApplication was created, or gtk_init() was called (the main thread).
If you want to take advantage of multi-threading in a GTK application, it is usually best to send long-running tasks to worker threads, and feed the results back to the main thread using g_idle_add() or GAsyncQueue. GIO offers useful tools for such an approach such as GTask.
Hi,
if call a widget function inside a goroutine, i get segfaults. F.e.
list
is*gtk.ListView
(field in a global struct)works, but:
breaks.
Any idea why?
Regards