On Tue, Jan 26, 2010 at 09:44:54PM -0800, Ryan Niebur wrote:
> attached is an example I wrote which demonstrates wx.CallAfter.

and, as usual, I forgot to attach it. this time it's attached.

Cheers,
Ryan

-- 
_________________________
Ryan Niebur
ryanrya...@gmail.com
import threading,wx

ID_BUTTON1=wx.NewId()
ID_BUTTON2=wx.NewId()

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        self.print_thread("Initial thread")
        wx.Frame.__init__(self, parent, ID, title)
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.foo = wx.TextCtrl(panel)
        sizer.Add(self.foo)
        sizer.Add(wx.Button(panel, ID_BUTTON1, "Click me"))
        sizer.Add(wx.Button(panel, ID_BUTTON2, "Click me2"))
        wx.EVT_BUTTON(self, ID_BUTTON1, self.button1_pressed)
        wx.EVT_BUTTON(self, ID_BUTTON2, self.button2_pressed)
        panel.SetSizer(sizer)

    def print_thread(self, name):
        print name + ": " + threading.currentThread().getName()

    def button1_pressed(self,event):
        threading.Thread(target=self.in_thread1).start()

    def button2_pressed(self,event):
        threading.Thread(target=self.in_thread2).start()

    # correct way
    def in_thread1(self):
        self.print_thread("New thread (from in_thread1)")
        wx.CallAfter(self.WriteMsg, "hi")

    # wrong way
    def in_thread2(self):
        self.print_thread("New thread (from in_thread2)")
        self.WriteMsg("hi")

    def WriteMsg(self,msg):
        self.print_thread("Writting in thread")
        self.foo.AppendText(msg)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "Multithreading example")
        frame.Show(True)
        return True

app = MyApp(0)
app.MainLoop()

Attachment: signature.asc
Description: Digital signature

Reply via email to