Jump to content

Initiate Function more than once


Go to solution Solved by BrewManNH,

Recommended Posts

Hello, I've been trying to get this script to not end upon executing. I currently use XBMC as a media center PC, and every now and then, when I close it to launch another program (such as a game), when said program is ended, XBMC does not start up as it should, or XBMC sometimes just freezes. 

As such, I created this script to get around that problem. It currently does what I want it to do. When someone holds escape for 5 seconds, it closes XBMC and relaunches it. However, this seems to be a 1 time deal. I cant have the script running and be able to continually accept escape being held for 5 seconds to re-run the function. I have to end the script and re-run the script, which doesn't seem ideal. 

Any suggestions would be appreciated! And I apologize for having to ask, my googling skills got me nowhere!

 

Here is the code:

#include <Misc.au3>
HotKeySet("{ESC}", "Terminate")
HotKeySet("{PAUSE}", "TogglePause")
Local $hDLL = DllOpen("user32.dll")
Global $Terminating = 0

If $CmdLineRaw Then
   ProcessClose ( "XBMC.exe" )
   While 1
       Sleep(100)
   WEnd
EndIf

Func TogglePause()
    $pause = Not $pause
    While $fPaused
        Sleep(100)
    WEnd
 EndFunc 
 
Func Terminate()
   If $Terminating = 1 Then
      Return 1
   EndIf
   $Terminating = 1
Local $Time = 0
 While _IsPressed("1B", $hDLL)
  $Time +=1
Sleep(400)
Beep (350*$Time, 100)
If $Time = 5 Then ExitLoop
  WEnd
  If $Time = 5 Then
      ProcessClose ( "XBMC.exe" )
   $Time = 0
 Run ( '"C:\Program Files (x86)\XBMC\XBMC.exe"')
 DllClose($hDLL)
  EndIf
  $Terminating = 0
EndFunc
Edited by nihouma
Link to comment
Share on other sites

  • Solution

You're exiting the script after your Run command restarts XBMC. Instead of using Exit, try using Return to see if that works the way you want it to.

Although, one other issue I see is that you have a While loop inside your Pause function with no way to exit it. $fPaused never gets updated anywhere, and you never set it to any value originally, so your pause function probably doesn't do anything anyways because $fPaused isn't set to anything that would activate the While loop.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I tried adding the return over exit, however, it does not work. I've also tried creating a second function (Terminate2) and using the HotKeySet("{ESC}", "Terminate2") that initiates from Terminate Function, but that did not work as well. 

 

And then, I realized I don't need a function at all (inspired by your advice as to my pause function, which was really only there because I thought adding that function would make my Terminate function work for some odd reason). I removed the function lines, made some changes, and the program works as I want it to! 

Basically, while holding escape down (which I have mapped to my xbox controllers home button), it begins incrementing $Time, once it reaches 5, it closes xbmc then restarts it. I've also tested adding in chrome and iexplorer all in one go, and it closes all of them and restarts xbmc fresh. Once it does that, $Time is reset, and the timer increments. If I release the escape button before it hits 5, it resets to 0. And it beeps to give me an auditory cue as to where $Time is at. 

Thank you so much for your help BrewManNH, I don't think I would've gotten to this point without your post! :)

#include <Misc.au3>
Local $hDLL = DllOpen("user32.dll")
Global $Terminating = 0
Global $Time = 0


  While 1
   If $Terminating = 1 Then
   EndIf
   $Terminating = 1

 While _IsPressed("1B", $hDLL)
  $Time +=1
Sleep(400)
Beep (350*$Time, 100)
If $Time = 5 Then ExitLoop
  WEnd
  If $Time = 5 Then
      ProcessClose ( "XBMC.exe" )
     Run ( '"C:\Program Files (x86)\XBMC\XBMC.exe"')
     $Time -= 5
     Sleep (800)
 DllClose($hDLL)
 DllOpen("user32.dll")
   EndIf
   $Time = 0
  $Terminating = 0
   WEnd
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...