Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would almost recommend not using thread, however, if you still want to do that, one very simple way to communicate between your main program and your thread is via threading events. This is really just a boolean event (on or off), and you can take action when the Event is set. Here is your original program updated to use this:</p> <pre><code>class RunMonitor(threading.Thread): def __init__(self, quit_event): threading.Thread.__init__(self) self.quit_event = quit_event def run(self): print 'Invoking the command prompt .....' subprocess.call(["start", "/DC:\\Scripts", "scripts_to_execute.bat"], shell=True) self.quit_event.set() def runscript(self): print 'Complete_file_Path inside Run script is : ' , self.complete_file_path file_operation.Generate_Bat_File(self.complete_file_path) quit_event = threading.Event() run_monitor_object = RunMonitor(quit_event).start() while True: if not quit_event.is_set(): print 'The thread is still alive....' else: print 'The Thread is not alive anymore' self.run_button.setEnabled(True) break </code></pre> <p>So, essentially, before you start the thread you create a <code>threading.Event()</code> object and pass that to your thread. Once this event is created you can <code>.set()</code> it to 'turn on' the Event, the main program simply waits for that to happen.</p> <p>Like I say, this is very simple, it's just a boolean event. If you need something more complex you could add more events, or use a <code>threading.Queue()</code> instead.</p> <p><strong>[EDIT]</strong> Here is the fully working sample I created (rather than try to shoe-horn everything into your sample):</p> <p>Here is the python file, note the changed to the <code>subprocess.call</code> line:</p> <pre><code>import threading import subprocess import time class RunMonitor(threading.Thread): def __init__(self, quit_event): threading.Thread.__init__(self) self.quit_event = quit_event def run(self): print 'Invoking the command prompt .....\n' subprocess.call(["start", "/WAIT", "/DC:\\python27\\sample", "xxx12.bat"], shell=True) self.quit_event.set() class Something: def runscript(self): print 'Starting the thread...' quit_event = threading.Event() run_monitor_object = RunMonitor(quit_event).start() while True: if not quit_event.is_set(): print 'The thread is still alive....' else: print 'The Thread is not alive anymore' break time.sleep(1) runme = Something() runme.runscript() </code></pre> <p>Note that I've added a sleep to the main loop so that the console doesn't fill up with "The thread is still alive..." messages.</p> <p>Also, for reference here is my batch file (I named it xxx12.bat, as referenced in the python code), I just used this to cause delays so I could prove the thread was terminating correctly:</p> <pre><code>echo wscript.sleep 2500 &gt; xxx12.vbs start /wait xxx12.vbs dir c:\ start /wait xxx12.vbs dir c:\ start /wait xxx12.vbs dir c:\ start /wait xxx12.vbs dir c:\ start /wait xxx12.vbs dir c:\ exit </code></pre> <p>The important thing to note here is that the 'exit' command in this batch file is vital, if we don't put that there the subprocess call will never terminate. Hopefully this example will help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload