-
-
Continuing the following post and as an answer to Jasper:
http://blogs.microsoft.co.il/blogs/asafshelly/archive/2010/03/02/c-close-previous-application-instance.aspx
The following code will make sure that only one instance is running by exiting if an instance is already running, and activating that instance.
[
DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static bool ActivateApplicationAlreadyRunning()
{
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length < 2) return (false);
foreach (Process process in processes)
{
if (process.Id != Process.GetCurrentProcess().Id)
{
SetForegroundWindow(process.MainWindowHandle);
return (true);
}
}
return false;
}
[STAThread]
static void Main()
{
if (ActivateApplicationAlreadyRunning()) return;
.....
-
-
Hi all,
Using my WinUSB C# component, I also needed to automatically install the driver if it is not already installed. The INF file defines a new device class by its GUID. This means that the class does not exist on the machine if the device is not installed.
Here is the code:
[
DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool SetupDiGetClassDescription(ref Guid ClassGuid,
StringBuilder classDescription, Int32 ClassDescriptionSize, ref UInt32 RequiredSize);
public static bool IsDeviceClassInstalled(string deviceClassGuid)
{
return (IsDeviceClassInstalled(new Guid(deviceClassGuid)));
}
public static bool IsDeviceClassInstalled(Guid deviceClassGuid)
{
StringBuilder deviceClassDescription = new StringBuilder(256);
UInt32 retLength = 0;
SetupDiGetClassDescription(ref deviceClassGuid, deviceClassDescription, deviceClassDescription.Capacity, ref retLength);
return (deviceClassDescription.Length > 0);
}
http://blogs.microsoft.co.il/blogs/asafshelly/archive/2009/12/29/winusb-net-component.aspx
-
-
Continuing this post:
http://kseesharp.blogspot.com/2008/11/c-check-if-application-is-already.html
It is possible to close the currently running instance of the application:
static bool CloseApplicationAlreadyRunning()
{
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length < 2) return (false);
foreach (Process process in processes)
{
if (process.Id != Process.GetCurrentProcess().Id)
{
process.CloseMainWindow();
if (process.WaitForExit(6000)) return (false);
else return (true);
}
}
return false;
}
Modify the timeout appropriately.
Asaf
http://asyncop.com/