为什么这种结束线程的方法不起作用?

发布于 2024-09-16 20:45:57 字数 2228 浏览 13 评论 0原文

我刚刚用我的菜鸟方式结束了一个线程,但我不知道为什么它不起作用。有人可以帮我吗?

这是我的示例代码:

import wx
import thread
import time
import threading

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, id = -1, title = "Testing", pos=(350, 110), size=(490, 200), style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX)
        self.panel = wx.Panel(self)

        self.stop = False

        self.StartButton = wx.Button(parent = self.panel, id = -1, label = "Start", pos = (110, 17), size = (50, 20))
        self.MultiLine = wx.TextCtrl(parent = self.panel, id = -1, pos = (38, 70), size = (410, 90), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_AUTO_URL)


        self.Bind(wx.EVT_BUTTON, self.OnStart, self.StartButton)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnStart(self, event):
        self.StartButton.Disable()
        self.NewThread = threading.Thread(target = self.LongRunning)
        self.NewThread.start()

    def OnClose(self, event):
        self.stop = True
        BusyBox = wx.BusyInfo("Just a moment please!", self)
        wx.Yield()

        while True:
            try:
                if not self.NewThread.isAlive():
                    self.Destroy()
                    break
                time.sleep(0.5)
            except:
                self.Destroy()
                break

    def LongRunning(self):
        Counter = 1

        while True:
            time.sleep(2)
            print "Hello, ", Counter
            self.MultiLine.AppendText("hello, " + str(Counter) + "\n") #If you comment out this line, everything works fine. Why can't I update the fame after I hit the close button?
            Counter = Counter + 1
            if self.stop:
                break

class TestApp(wx.App):
    def OnInit(self):
        self.TestFrame = TestFrame()
        self.TestFrame.Show()
        self.SetTopWindow(self.TestFrame)
        return True

def main():
    App = TestApp(redirect = False)
    App.MainLoop()

if __name__ == "__main__":
    main()

正如您在我的代码中看到的,线程中有一个无限循环,我告诉线程要做的是一旦单击关闭按钮就跳出循环。但问题是,每次我点击关闭按钮时,代码似乎都停留在 self.MultiLine.AppendText("hello, " + str(Counter) + "\n") 行,我不知道为什么。有人可以帮忙吗?

I just came out with my noob way of ending a thread, but I don't know why it's not working. Would somebody please help me out?

Here's my sample code:

import wx
import thread
import time
import threading

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, id = -1, title = "Testing", pos=(350, 110), size=(490, 200), style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX)
        self.panel = wx.Panel(self)

        self.stop = False

        self.StartButton = wx.Button(parent = self.panel, id = -1, label = "Start", pos = (110, 17), size = (50, 20))
        self.MultiLine = wx.TextCtrl(parent = self.panel, id = -1, pos = (38, 70), size = (410, 90), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_AUTO_URL)


        self.Bind(wx.EVT_BUTTON, self.OnStart, self.StartButton)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnStart(self, event):
        self.StartButton.Disable()
        self.NewThread = threading.Thread(target = self.LongRunning)
        self.NewThread.start()

    def OnClose(self, event):
        self.stop = True
        BusyBox = wx.BusyInfo("Just a moment please!", self)
        wx.Yield()

        while True:
            try:
                if not self.NewThread.isAlive():
                    self.Destroy()
                    break
                time.sleep(0.5)
            except:
                self.Destroy()
                break

    def LongRunning(self):
        Counter = 1

        while True:
            time.sleep(2)
            print "Hello, ", Counter
            self.MultiLine.AppendText("hello, " + str(Counter) + "\n") #If you comment out this line, everything works fine. Why can't I update the fame after I hit the close button?
            Counter = Counter + 1
            if self.stop:
                break

class TestApp(wx.App):
    def OnInit(self):
        self.TestFrame = TestFrame()
        self.TestFrame.Show()
        self.SetTopWindow(self.TestFrame)
        return True

def main():
    App = TestApp(redirect = False)
    App.MainLoop()

if __name__ == "__main__":
    main()

As you can see in my code, there's a infinite loop in the thread, what I tell the thread to do is break out of the loop once I click the close button. But the problem is, every time when I hit the close button, it seems the code stuck at self.MultiLine.AppendText("hello, " + str(Counter) + "\n") line, I don't know why. Anybody can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2024-09-23 20:45:57

更新多行时尝试使用线程安全方法,例如 wx.CallAfter

 def LongRunning(self):
     Counter = 1

     while True:
         time.sleep(2)
         print "Hello, ", Counter

         wx.CallAfter(self.updateMultiLine, "hello, " + str(Counter) + "\n")
         Counter = Counter + 1
         if self.stop:
             break

 def updateMultiLine(self, data):
     self.MultiLine.AppendText(data)

Try using a thread safe method such as wx.CallAfter when updating your multiline.

 def LongRunning(self):
     Counter = 1

     while True:
         time.sleep(2)
         print "Hello, ", Counter

         wx.CallAfter(self.updateMultiLine, "hello, " + str(Counter) + "\n")
         Counter = Counter + 1
         if self.stop:
             break

 def updateMultiLine(self, data):
     self.MultiLine.AppendText(data)
讽刺将军 2024-09-23 20:45:57

一般来说,对于 GUI 工具包,只有一个线程应该访问 GUI 函数。一个例外是 wx.CallAfter

正如您(应该)知道的,软件缺陷可以分为三组:

  1. 你的虫子。
  2. 他们的错误。
  3. 线程。

;)

In general with GUI toolkits, only one thread should access GUI functions. An exception is wx.CallAfter

As you (should) know, software defects can be classified into three groups:

  1. Your bugs.
  2. Their bugs.
  3. Threads.

;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文