登录
首页 >  Golang >  Go问答

显示 GUI 时在后台处理函数

来源:stackoverflow

时间:2024-04-05 23:27:37 242浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《显示 GUI 时在后台处理函数》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我正在尝试修复此问题以使其正常工作,我希望客户端在后台 initconnection() 并接收命令并在显示命令为“closegui”的消息窗口时处理它们..

func initconnection() {
    dialer, err := proxy.socks5("tcp", "127.0.0.1:9050", nil, proxy.direct)
    if err != nil {
        log.fatal("failed to connect via socks5 proxy:", err)
    }

    conn, err := dialer.dial("tcp", "localhost")
    if err != nil {
        log.fatal("failed to connect to server:", err)
    }
    defer conn.close()

    _, err = conn.write([]byte(fmt.sprintf("connected")))
    if err != nil {
        log.println("error sending information to server:", err)
        return
    }

    fmt.println("connected to server.")

    scanner := bufio.newscanner(conn)
    for scanner.scan() {
        command := scanner.text()
        commandandparam := strings.split(command, ",")

        receivedcommand := commandandparam[0]

        switch receivedcommand {
        case "opengui":
            drawmessage()
            //the problem is when calling drawmessage() and the gui shows the client not processing the command anymore until the gui is closed which is a problem for me because i want to close the gui by commands from the server
        case "closegui":
            // here i want to hide the gui and set
            messageallowed = false
        }
        // execute the command
        output, err := executecommand(receivedcommand)
        if err != nil {
            log.println("error executing command:", err)
            continue
        }

        // send the command output to the server
        _, err = conn.write([]byte(output + "\n"))
        if err != nil {
            log.println("error sending output to server:", err)
            break
        }
    }

    if scanner.err() != nil {
        log.println("error reading input:", scanner.err())
    }

    time.sleep(100 * time.millisecond)
}

gui功能代码:

func drawmessage() {
    // create a new fyne application
    myapp := app.new()
    // create a new window
    mywindow := myapp.newwindow("message from the server!")

    // create the ransom note message
    note := widget.newlabel("hello world")
    
    content := container.newvbox(
        note,
    )

    // set the content of the window
    mywindow.setcontent(content)

    // show the window
    mywindow.showandrun()
}

这是第二次调用的主要函数,我想检查 messageallowed bool 是否显示消息,即使客户端关闭,我也希望保存它,以便当客户端重新连接时再次显示是否允许

func main() {
    initConnection()
    if src.MessageAllowed == true {
        go DrawMessage()
    }
}

所以最终我希望客户端接收命令并在显示消息时在后台处理它,并且我想让客户端检查与服务器的最后一次会话是否允许该消息


正确答案


Fyne(或任何 GUI 工具包)无法在后台运行 - 但您的处理程序可以。 在 initConnection 前面使用“go”关键字,而不是 DrawMessage

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>