无法访问线程内的参数

时间:2014-01-02 11:00:26

标签: multithreading python-2.7 python-multithreading enterprise-architect

我有一个函数,我想在一个线程中调用它。代码如下。我的问题是,当我覆盖init函数时,我能够访问参数(所有类型),但是当控件运行时,参数不可访问(只有strnig参数可访问,其他2是Colelction类和Repository类对象)。下面的代码是我想要实现的目标的演示。

class ExportModel(threading.Thread):
    def __init__ (self,Collection,repo,SelectedPackage,childLayerPackage):
        self.Collection = Collection
        self.repo = repo
        self.SelectedPackage = SelectedPackage
        self.childLayerPackage = childLayerPackage

        MessageBox(None, str(self.SelectedPackage), 'Window title',0)#Displays PackageName
        MessageBox(None, str(self.childLayerPackage), 'Window title',0)#Displays Child PackageName
        MessageBox(None, str(self.Collection.Name), 'Window title',0)#Displays Collection Name
        MessageBox(None, str(self.repo.Count), 'Window title',0)#Displays 10


        threading.Thread.__init__( self )

    def run(self):

        MessageBox(None, str(self.SelectedPackage), 'Window title',0)#Displays PackageName
        MessageBox(None, str(self.childLayerPackage), 'Window title',0)#Displays Child PackageName
        MessageBox(None, str(self.Collection.Name), 'Window title',0)#Doesnt popup a message box
        MessageBox(None, str(self.repo.Count), 'Window title',0)#Doesnt popup a message box

if __name__ == "__main__":

   ExportModel(Collection,m_Repository,"","").start()

1 个答案:

答案 0 :(得分:0)

我认为问题在于您没有加入创建消息框的线程。主线程不会等待您生成的线程完成,程序会在弹出所有消息框之前终止。试试这个:

if __name__ == "__main__":
    t = ExportModel(Collection,m_Repository,"","")
    t.start()
    t.join()