Scripts - "run <application>" background processing

Discuss and share scripts and script files...
Post Reply
highend
Posts: 9269
Joined: 06 Feb 2011 00:33

Scripts - "run <application>" background processing

Post by highend » 18 Jan 2012 11:25

Hi,

I just wrote (a very simple) AutoIt v3 script that "communicates" with one of my XYplorer scripts via the clipboard.
What's the reason to do this?

I haven't found a reasonable way to execute a 'run <application>' script command that waits until it's finished before it continues with the next instruction WITHOUT blocking the XYplorer's GUI (although it should run in the background).

Here is a link for a compiled version (.exe) of the script so that you don't need to install AutoIt yourself (or you can use the portable version including SciTE to do it manually: http://www.autoitscript.com/forum/topic ... __portable).

http://db.tt/sah2EybV

What are the scripts doing:

The XYplorer's script starts the CheckProcess.exe file (the compiled .au3 script), executes the "run <application" command and then copies the text (e.g.) "WinRAR.exe=started" to the clipboard. After that it will poll the clipboard every 250 ms to see if it holds the string "WinRAR.exe=finished". If that's the case it continues with the next instruction.

The AutoIt script get's the process name as it's first argument, checks the clipboard for "WinRAR.exe=started" (250 ms interval) and when found it waits for the process's end to write "WinRAR.exe=finished" back to the clipboard.

I didn't like the way to use a temp file for this kind of task because of the constant disk activity but if you use a clipboard manager and don't like the entries that are created you should modify both scripts to use a file instead.

I haven't found an issue so far but if you'd like to suggest a different or better method, don't hesitate to do so :)

In my WinRAR script I'll use it like this:

Code: Select all

	global $ProcessName;
	$CheckProcess = "<xypath>\.Tools\CheckProcess\CheckProcess.exe";

	// Start the process watcher (external tool)
	$ProcessName = "WinRAR.exe";
	run """$CheckProcess"" ""$ProcessName""";

	// Actually extract all archive files (-kb = keep broken files | -o+ = overwrite existing files)
	run """$WinRARCmd"" x -kb -o+ ""$SourceFileWildcardName"" ""$FilePath\$ShortedFileName\""", , 0;

	// Inform the process watcher tool
	copytext "$ProcessName=started";

	// Wait until WinRAR has finished it's task(s)
	sub "_CheckProcess";

// Wait until a process has finished
"_CheckProcess"
	global $ProcessName;

	$WaitForProcess = "-1";
	while($WaitForProcess == -1){
		$WaitForProcess = strpos("<clipboard>", "$ProcessName=finished");
		wait 250;
	}
The AutoIt script:

Code: Select all

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Res_Icon_Add=D:\Tools\.AutoIt\App\Icons\au3.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#comments-start
	The script expects a process name (with or without quotation marks) as it's only parameter.
#comments-end

Opt("ExpandVarStrings", 1) ; Expand variables in strings

If $CmdLine[0] = 0 Then
   $ProcessName = ""
   Exit
Else
   $ProcessName = $CmdLine[1]
EndIf

$WaitForProcess = 0;
While $WaitForProcess = 0
   Sleep(250)
   $ClipBoard = ClipGet()
   $WaitForProcess = StringInStr("$ClipBoard$", "$ProcessName$=started", 2)
WEnd

ProcessWaitClose("$ProcessName$")
ClipPut("$ProcessName$=finished")
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

Twisten
Posts: 199
Joined: 27 Apr 2008 10:30

Re: Scripts - "run <application>" background processing

Post by Twisten » 18 Jan 2012 17:25

Off the top of my head I don't see a way to do it without accessing the drive either for a temp file or to run a program that'll check if the process is still running (unlike a monitor already loaded to memory).

A very good improvement would be for the run command to firstly have a return code which could take the same approach winrar does in 'setup' mode, return the executed command return code * 1000 to differentiate the times it has to return its own error code; And secondly to have a 'wait' option that doesn't lock the XYplorer GUI, though it would require some careful scripting to avoid unexpected results in scripts when the context (like current folder, focused/selected items etc.. ) suddenly (for the paused script) changes.

klownboy
Posts: 3005
Joined: 28 Feb 2012 19:27

Re: Scripts - "run <application>" background processing

Post by klownboy » 28 Jun 2012 13:56

Hi highend,
I successfully :D used your checkprocess autoit script in a small XYplorer script of my own. I had issues initially because I used what I thought was a newer version in your other post on "Extractor" http://www.xyplorer.com/xyfc/viewtopic. ... lit=autoit, but I continually received an error message on "Not enough command line arguments, aborted!". I finally realized that even though the name is the same they are 2 different "checkprocess.exe" files.
As I said I got my script to work fine once I had used the version in this post. I was wondering though when one wants to continue executing additional lines in the XYplorer script where is the most appropriate place to do that? Is it right after the sub "_CheckProcess"; line (as I did) or after the sub routine...after line wait 250; } ( at the very end of the script)?

Code: Select all

   // Wait until WinRAR has finished it's task(s)
   sub "_CheckProcess";

// Wait until a process has finished
"_CheckProcess"
   global $ProcessName;

   $WaitForProcess = "-1";
   while($WaitForProcess == -1){
      $WaitForProcess = strpos("<clipboard>", "$ProcessName=finished");
      wait 250;
   }
Thanks,
Ken

highend
Posts: 9269
Joined: 06 Feb 2011 00:33

Re: Scripts - "run <application>" background processing

Post by highend » 28 Jun 2012 15:01

I finally realized that even though the name is the same they are 2 different "checkprocess.exe" files.
Exactly. The version that is "bundled" inside Extractor uses a temp file instead of the clipboard. It's a better approach because of password protected archives.
Is it right after the sub "_CheckProcess";
Yes, that's the right place.
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

Post Reply