Reputation: 23831
All, I am developing an application that needs to launch another application at run-time. To launch the third-party app I am using System.Diagnostics.Process
and to ensure I never launch the third-party application twice I employ a singleton pattern.
The singleton is working is required, but Process.Start()
method is not. That is although I am getting the same Process
object returned from the singleton, Start()
is launching another instance of the third-part app.
From MSDN - Process.Start() page:
"Starts (or reuses) the process resource that is specified by the StartInfo property
of this Process component and associates it with the component."
suggest that it should reuse the instance of the Process
. What am I missing?
Thanks for your time.
Upvotes: 1
Views: 983
Reputation: 6490
use it as follows
Process[] chromes = Process.GetProcessesByName("ProcessName"); to check whether ur process is already running or not. Based on the u can use the process already running.
Upvotes: 0
Reputation: 505
Here is the function I use to start third party applications:
public static void ProcessStart(string ExecutablePath, string sArgs, bool bWait)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = ExecutablePath;
if(sArgs.Length > 0)
proc.StartInfo.Arguments = sArgs;
proc.Start();
if(bWait)
proc.WaitForExit();
if(ProcessLive(ExecutablePath))
return true;
else
return false;
}
ExecutablePath: Full path to the executable
sArgs: Command line arguments
bWait: Wait for the process to exit
In my case I use a secondary function to determine if the process is already running. This is not exactly what you are looking for, but it will still work:
public static bool ProcessLive(string ExecutablePath)
{
try
{
string strTargetProcessName = System.IO.Path.GetFileNameWithoutExtension(ExecutablePath);
System.Diagnostics.Process[] Processes = System.Diagnostics.Process.GetProcessesByName(strTargetProcessName);
foreach(System.Diagnostics.Process p in Processes)
{
foreach(System.Diagnostics.ProcessModule m in p.Modules)
{
if(ExecutablePath.ToLower() == m.FileName.ToLower())
return true;
}
}
}
catch(Exception){}
return false;
}
Upvotes: 1
Reputation: 406
Perhaps you should consider using Process.GetProcessesByName
to understand if that application you're launching is already running.
Upvotes: 1