Game Development Reference
In-Depth Information
}
public static bool IsRunning(System.Reflection.Assembly assembly)
{
// Add 1 to account for the application doing the check
return Query(assembly) > 1;
}
private static int QueryRunningProcessCount(string applicationName)
{
// Build a formatted WMI management query to select all
// processes matching a specific name
string query = String.Format(“SELECT Name FROM CIM_Process “ +
“WHERE Name = '{0}'”,
applicationName);
// Build an enumerator for the management query results
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
// Return the number of results (process count) in the management query
return searcher.Get().Count;
}
}
The WMI implementation has a class composed of static methods that can deter-
mine the number of running processes filtered by name. The easiest and most
maintainable way to use this implementation is to pass the executing assembly
object into the manager. This way, if the assembly name changes, you do not have
to update the code to reflect these changes. The following code shows the proper
usage of the WMI approach.
[STAThread]
static void Main()
{
if (ProcessCountManager.IsRunning(Assembly.GetExecutingAssembly()))
{
MessageBox.Show(“There is already an instance of this “ +
“executable running as a process”);
}
else
Application.Run(new MainForm());
}
Search Nedrilad ::




Custom Search