Gatan | AMETEKSkip to Main Content
No options found

Launching an external application and regularly checking if it is still running

DigitalMicrograph Script

Example script showing how the external application Notepad.exe can be launched. The script regularly polls whether or not Notepad.exe is still open so that actions can be performed once the external application is closed.

Preview

class CExternalProcessSentry
{
	number PID
	number periodicTaskID
	
	CExternalProcessSentry( object self ) { Result("\n Debug Info: Created object " + self.ScriptObjectGetID() ); }
	~CExternalProcessSentry( object self ) { Result("\n Debug Info: Removed object " + self.ScriptObjectGetID() ); }
	
	void Launch( object self, string command, number checkInterval_sec )
	{
		// Launch external application and
		// continue script immediately 
		PID = LaunchExternalProcessAsync( command )	
		
		if ( 0 == PID )
			Throw( "Could not launch external command.\n\n" + command )
			
		// Register periodic checking
		periodicTaskID = AddMainThreadPeriodicTask( self, "CheckAndNotify", checkInterval_sec )
	}
	
	void CheckAndNotify( object self )
	{
		if ( IsExternalProcessActive( PID ) )
		{
			Result( "\n\t The external process [" + PID +"] is still active @" + GetTime(1) )
		}
		else
		{
			Result( "\n\t The external process [" + PID +"] is no longer active." )
			
			// Unregister periodic checking
			RemoveMainThreadTask( periodicTaskID )
		}
	}
}

// Launches NotePad application and check every 5 sec if it is still running.
string msg = "notepad.exe"	
number checkTime = 5
Alloc( CExternalProcessSentry ).Launch( msg, checkTime )