SoFunction
Updated on 2025-03-07

Example of method to start another program using C#


private void btnCreate_Click(object sender, EventArgs e)
...{
int hWnd = FindWindow(null, "test");//The name of the window
//check if PowerReuse is launched or not
//if yes, pass path of project to PowerReuse
//or, launch PowerReuse with specified parameter
if (hWnd > 0)
...{
("powerReuse has been launched already." + " " + ());
//SendMessage to PowerReuse
return;
}
try
...{
Process Main_P = new Process();
//this path should be retrieved from Windows Registry,
//the loaction is written by Installter during process of installation.
Main_P. = @"C: ";//The exe route of running
//This URL is passed to PowerReuse to open
Main_P. = @"C:";// Parameters for running
Main_P. = true;
Main_P.Start();
//
//we have to wait for a while until UI has been initialized
//
Main_P.WaitForInputIdle(10000);
//although UI has been initialzied,
//it does not mean main form of application has been completed.
//we may wait for another 10 seconds
for (int i = 0; i < 100; i++)
...{
hWnd = FindWindow(null, "PowerReuse (Beta)");
//hWnd = Main_P.MainWindowHandle.ToInt32() ;
if (hWnd > 0) break;
(100);
}
//Here, we check if PowerReuse is fully launched
if (hWnd == 0)
...{
//Handle exception
("We cannot find window handle of PowerReuse");
}
else
...{
//other handling
//
(() + " " + Main_P.() + " " + Main_P.MainWindowTitle);
}
}
catch (Exception ex)
...{
();
}
}