擅长:python、mysql、java
<p>我读了Stack上的其他问题,但在跨类通信方面还是有点困惑。以下是我如何接近它:</p>
<p>我使用一个列表来保存wxFrame类的<code>__init__</code>方法中的所有线程:<code>self.threads = []</code></p>
<p>按照<a href="https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python">How to stop a looping thread in Python?</a>中的建议,在初始化线程类时,我在线程类中使用设置为<code>True</code>的信号。</p>
<pre><code>class PingAssets(threading.Thread):
def __init__(self, threadNum, asset, window):
threading.Thread.__init__(self)
self.threadNum = threadNum
self.window = window
self.asset = asset
self.signal = True
def run(self):
while self.signal:
do_stuff()
sleep()
</code></pre>
<p>我可以通过遍历线程来停止这些线程:</p>
<pre><code>def OnStop(self, e):
for t in self.threads:
t.signal = False
</code></pre>