twgh / xcgui

炫彩界面库. Go GUI library. Golang bindings for XCGUI, Windows GUI library, DirectUI design idea.

Home Page:https://pkg.go.dev/github.com/twgh/xcgui

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fatal error: too many callback functions

wengooooo opened this issue · comments

不停的调用XC_CallUiThread会报fatal error: too many callback functions错误

这两个帖子好像也提到了超过2000次后会出问题
https://stackoverflow.com/questions/66864829/golang-fatal-error-too-many-callback-functions
golang/go#1912

复现代码

package main

import (
	"fmt"
	"github.com/twgh/xcgui/app"
	"github.com/twgh/xcgui/widget"
	"github.com/twgh/xcgui/window"
	"github.com/twgh/xcgui/xc"
	"github.com/twgh/xcgui/xcc"
	"time"
)

func main() {

	a := app.New("")

	win := window.NewWindow(0, 0, 466, 300, "炫彩窗口", 0, xcc.Xc_Window_Style_Default)

	logEdit := widget.NewEdit(100, 100, 100, 100, win.Handle)

	btn_test := widget.NewButton(250, 150, 50, 50, "点击", win.Handle)
	btn_test.Event_BnClick(func(pbHandled *bool) int {
		*pbHandled = true
		go func() {
			// 不断的调用会报错
			for ;; {
				xc.XC_CallUiThread(func(data int) int {
					timeStyle := logEdit.AddStyleEx("微软雅黑",10, 0, xc.RGB(255, 255, 255), true)
						logEdit.AddTextEx(fmt.Sprintf("%s: ", time.Now().Format("2006-01-02 15:04:05")), timeStyle)
					return 0
				}, 1)
				time.Sleep(500 * time.Microsecond)
			}
		}()
		return 0
	})
	// 3.显示窗口
	win.ShowWindow(xcc.SW_SHOW)
	// 4.运行程序
	a.Run()
	// 5.释放UI库
	a.Exit()
}

commented

答案是不使用匿名函数即可。
5a1ekd.png

package main

import (
	"github.com/twgh/xcgui/app"
	"github.com/twgh/xcgui/widget"
	"github.com/twgh/xcgui/window"
	"github.com/twgh/xcgui/xc"
	"github.com/twgh/xcgui/xcc"
	"strconv"
	"time"
)

var (
	logEdit *widget.Edit
	i       = 1
)

func main() {
	a := app.New("")
	win := window.NewWindow(0, 0, 400, 220, "炫彩窗口", 0, xcc.Xc_Window_Style_Default)
	logEdit = widget.NewEdit(90, 50, 70, 30, win.Handle)

	btn_test := widget.NewButton(30, 50, 50, 30, "点击", win.Handle)
	btn_test.Event_BnClick(func(pbHandled *bool) int {
		*pbHandled = true
		go func() {
			for {
				// 不使用匿名函数
				xc.XC_CallUiThread(fun, 1)
				time.Sleep(500 * time.Microsecond)
			}
		}()
		return 0
	})

	win.ShowWindow(xcc.SW_SHOW)
	a.Run()
	a.Exit()
}

func fun(data int) int {
	logEdit.SetText(strconv.Itoa(i))
	logEdit.Redraw(true)
	i++
	return 0
}

如果不使用匿名函数的话,传递自定义参数好像就得定义一个全局变量,因为这个data传递不了整形指针进去

commented

如果不使用匿名函数的话,传递自定义参数好像就得定义一个全局变量,因为这个data传递不了整形指针进去

可以把数据放到map或数组里,传索引进去,总之需要频繁调用的回调函数还是不要用匿名函数了。

问题解决。多谢