views:

71

answers:

2

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?

+1  A: 

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.

;)

Ivo van der Wijk
I just disabled the start button once a thread starts, that's one thread accessing GUI functions, still have the same problem... Would somebody give me a detailed reason?
Shane
@Shane: "one thread" doesn't mean "one thread at a time". It means that whatever thread starts the event loop should be the only thread that ever directly updates the UI. You can't create a separate thread and then switch to it being the one that updates the UI.
Bryan Oakley
+2  A: 

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)
volting
@volting: You have answered quite a few of my dummy questions, so thank you very much for you patience!
Shane
@Shane: Your welcome, always glad to help! :)
volting