Gatan | AMETEKSkip to Main Content
No options found

Having a script wait for a file to appear

DigitalMicrograph Script

Example script which holds its execution until a certain file is found at a given location. Checking is performed on the main thread at given intervals.

Preview

// Example script which holds its execution until a certain file is found
// at given location. Checking is performed on the main thread at given intervals.
// The script will continue/end once a file is found at "C:\TestDummy.txt" )
// This type of checking can be used to have external applications "communicate" with
// DigitalMicrograph scripts.

class CWaitOnFileAction
{
	object fileReadySignal
	string path
	number taskID
	
	void CheckForFile( object self )
	{
		if ( DoesFileExist( path ) )
			fileReadySignal.SetSignal()
	}
	
	object init( object self, string checkPath, number checkInterval_sec )
	{
		fileReadySignal = NewSignal( 0 )
		path = checkPath
		taskID = AddMainThreadPeriodicTask( self, "CheckForFile", checkInterval_sec )
		return self
	}
	
	void DoActionWhenFileReady( object self )
	{
		Result( "\n Doing things...." )
		Result( "\n Now wait on 'external' file (no timeout): " + path )
		fileReadySignal.WaitOnSignal( infinity(), NULL )
		Result( "\n ..file found. Continue work..." )
		RemoveMainThreadTask( taskID )	// Stopp checking regularly...
	}
}

Alloc(CWaitOnFileAction).Init( "C:\\TestDummy.txt", 5 ).DoActionWhenFileReady()