Kodi Community Forum

Full Version: How to kill a process (via dialog box) while waiting?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I tried something like this:

Code:
pDialog = xbmcgui.DialogProgress()
pDialog.create('XBMC', 'Press Cancel to Exit')
Progmain = subprocess.Popen(<Program & Parameters>)
Progmain.wait()
   if (pDialog.iscanceled()):
      os.kill(Progmain)


Obviously that didnt work. I want the running program to terminate when I hit cancel, can anyone help?
os.kill do not work because this function do only operate on pid and signals.
I guess sys.exit is may better
Or try: sys.modules.clear()
Well my code obviously can't work because it waits for the program to close before checking the dialog box. Any more ideas?
RandomXBMCUser Wrote:Well my code obviously can't work because it waits for the program to close before checking the dialog box. Any more ideas?

I guess you could determine the state of the process inside the loop.

Code:
#########################################################
# Function  : OSRun                                     #
#########################################################
# Parameter :                                           #
#                                                       #
# command     command to execute over ssh               #
# backg       boolean :                                 #
#             - true  command is put into background &  #
#             - false command is not startet in         #
#               background (very dangerous .... )       #
# busys       boolean :                                 #
#             - show busy-dialog during the operation   #
#                                                       #
# Returns   :                                           #
#                                                       #
#             rc-val from os.system call                #
#                                                       #
#########################################################
def OSRun(command,backg,busys):

    global __configLinux__
    global __verbose__

    if (busys):
        xbmc.executebuiltin("ActivateWindow(busydialog)")
    sys.platform.startswith('linux')


    sshlog ="echo \"" + command + "\" >> " + __configLinux__[38]
    status = os.system("%s" % (sshlog))

    if (__verbose__ == 'true'):
        OSlog("Command to log inside ssh:" + sshlog)
        OSlog ("OSRun start")


    commandssh = "ssh " + __configLinux__[6] + " " + __configLinux__[40] + command + " "

    if (backg):
        commandssh = commandssh + " > /dev/null 2>&1 &"

    status = os.system("%s" % (command))

    if (__verbose__ == 'true'):
        OSlog("Command to run :" + commandssh)

    # No we execute the command  ...
    # over ssh

    status = os.system("%s" % (commandssh))

    if (busys):
        xbmc.executebuiltin("Dialog.Close(busydialog)")

    if (__verbose__ == 'true'):
        OSlog ("OSRun end")

    return status

#########################################################
I tried this code:

Code:
aDialog = xbmcgui.DialogProgress()
  aDialog.create('XBMC', 'Press Cancel to Stop Program...')
  myProg = subprocess.Popen(<Program Parameters>)
  progcheck = 0
  while progcheck == 0:
      time.sleep(2)
      retcode = myProg.Poll()
      if (aDialog.iscanceled()):
        sys.close(myProg)
        progcheck = 1
      if retcode:
        progcheck = 1

But the dialog box instantly closes when the polling of the program starts. Any suggestions?
Eek
I'm working on the Windows platform, btw.
One last plea for someone to let me know whats wrong with my code...last bump, I promise!

Quote:But the dialog box instantly closes when the polling of the program starts. Any suggestions?

Code:
aDialog = xbmcgui.DialogProgress()
  aDialog.create('XBMC', 'Press Cancel to Stop Program...')
  myProg = subprocess.Popen(<Program Parameters>)
  progcheck = 0
  while progcheck == 0:
     time.sleep(2)
     retcode = myProg.poll()
     if (aDialog.iscanceled()):
       sys.close(myProg)
       progcheck = 1
     if retcode:
       progcheck = 1