Processing Ajax...
Save 25% on our desktop apps during our Black Friday/Cyber Monday sale!Save 25% on our desktop apps during our Black Friday/Cyber Monday sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save 25% on our desktop apps during our Black Friday/Cyber Monday sale!

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Do stuff on process start/end

Description
This script will loop in the background, looking for a process. It will do stuff when it detects the process has started, and do other stuff when it detects the process is gone. Set your stuff to do on lines 36 and 53.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
-
Date Created
Apr 29, 2022
Date Last Modified
Apr 29, 2022

Scripted Function (Macro) Code

using System;
using System.Drawing;

public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		// Set the process name here (case sensitive)
		string processName = "*notepad++.exe";

		// Set the process checking timeout (in milliseconds) here
		uint processCheckWaitTime = 10000;

		// Set the "cooldown" timeout (in milliseconds) here
		uint cooldownTime = 30000;

		// Loop forever looking for the process
		while (true)
		{
			// DEBUG
			BFS.General.LogText("Script: \"Do stuff on process start/end\" main loop starting (process exists check)...");

			// If it's found do stuff
			if (BFS.Application.IsAppRunningByFile(processName))
			{
				BFS.General.LogText("Script: \"Do stuff on process start/end\" found process...");

				// Wait 30 seconds to make sure it's still around
				// This prevents these actions from firing multiple times if the process closes/re-opens quickly
				BFS.General.ThreadWait(cooldownTime);
				if (BFS.Application.IsAppRunningByFile(processName))
				{
					BFS.General.LogText("Script: \"Do stuff on process start/end\" process still found, doing the process started stuff...");

					// Stuff you want to do when the process has started
					BFS.DisplayFusion.RunFunctionAndWait("Function Name");

					// Now periodically check if it's still running, then do stuff when it's not
					while (true)
					{
						BFS.General.LogText("Script: \"Do stuff on process start/end\" process gone loop starting...");

						if (!BFS.Application.IsAppRunningByFile(processName))
						{
							// Wait 30 seconds to make sure it's gone for good
							BFS.General.ThreadWait(cooldownTime);

							if (!BFS.Application.IsAppRunningByFile(processName))
							{
								BFS.General.LogText("Script: \"Do stuff on process start/end\" process still gone, doing stuff...");

								// Stuff you want to do when the process has ended
								BFS.DisplayFusion.RunFunctionAndWait("Function name");

								// Exit the process ended check loop
								break;
							}
						}
						else
						{
							// Wait 10 seconds before restarting the process ended loop
							BFS.General.ThreadWait(processCheckWaitTime);
						}
					}
				}
			}

			// Wait 10 seconds before restarting the process started loop
			BFS.General.ThreadWait(processCheckWaitTime);
		}
	}
}