2012-01-27 91 views
0

我应该继续使用这样的线程还是应该使用多处理?我试图让按钮按下时切换while循环。切换线程

主题:

class workingthread(threading.Thread): 
    def __init__(self): 
     threading.Thread.__init__(self) 

    def run(self): 
     while 1: 
      chat = skype.CreateChatWith(name) 
      chat.SendMessage(message) 
      t.sleep(timeou) 

启动线程:

def this(self,event): 
    t = workingthread() 
    t.start() 
+0

有谁知道吗? – user1152873 2012-01-28 00:25:34

+0

我认为你没有提供足够的解释给任何人提供帮助。 – Jivings 2012-01-28 17:03:10

+0

我会编辑我的帖子并添加更多信息。 – user1152873 2012-01-28 19:28:43

回答

0

你的问题有点不清楚。我试图专注于“我试图让按钮按下”部分切换while循环。据我的理解,你想一直有线程活动(通过活动,我认为不杀死线程),但你只是偶尔有执行while循环体。最简单的解决方案是实现扩大workingthread这样的布尔变量和:

class workingthread(threading.Thread): 
    def __init__(self): 
     threading.Thread.__init__(self) 
     self.doit = True 

    def run(self): 
     while 1: 
      if doit: 
       # if the execution of both statements is desired 
       chat = skype.CreateChatWith(name) 
       chat.SendMessage(message) 
      # sleep in both cases 
      t.sleep(timeou) 

现在你可以使用一个按键事件(无论UI工具包您正在使用),一个方法绑定到它,并且可以切换doit - 变量的workingthread。取决于doit,while循环体是否被执行。