Explaination: Window Code

G

Guest

In the following code snipet, what does this code do? Where does it return a
bool to... OS? Does it import a User32.dll library? The code is part of
creating a singleton [tempate] instance of an appication. The complete code
is located at
http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp


//the Process is the appl that's aready running -want ony one instance
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}

[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private const int WS_SHOWNORMAL = 1;
}
 
A

Arne Janning

Hi Steve,
In the following code snipet, what does this code do? Where does it
return a
bool to... OS? Does it import a User32.dll library? The code is part of
creating a singleton [tempate] instance of an appication. The complete
code
is located at
http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp


//the Process is the appl that's aready running -want ony one instance
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}

[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private const int WS_SHOWNORMAL = 1;
}

Almost the same code can be found here:
http://www.bobpowell.net/singleinstance.htm
The code is explained there.

A better possibility to make sure that only one instance of your application
runs at a time using a mutex:
http://www.yoda.arachsys.com/csharp/faq/#one.application.instance

Cheers

Arne Janning
 
G

Guest

Thank you for your response. I'm digesting the info now, great web sites.
My 2 requirements follow:

1. I have a network application. I want users to start the application
from the network but only allow one instance of the application to run on
EACH users machine while, at the same time, allowing various users [on the
network] to execute the application (i.e. different machines).

2. However, within the application is a database (MS-Access) ADO stuff
(DataGrid, etc). For these database forms, in the application, I want only
one network instance running because I'm afraid IF muliple users are saving
or changing database records it will create DataSet problems, etc.

Or I'm I missing something?

Steve

Arne Janning said:
Hi Steve,
In the following code snipet, what does this code do? Where does it
return a
bool to... OS? Does it import a User32.dll library? The code is part of
creating a singleton [tempate] instance of an appication. The complete
code
is located at
http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp


//the Process is the appl that's aready running -want ony one instance
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}

[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private const int WS_SHOWNORMAL = 1;
}

Almost the same code can be found here:
http://www.bobpowell.net/singleinstance.htm
The code is explained there.

A better possibility to make sure that only one instance of your application
runs at a time using a mutex:
http://www.yoda.arachsys.com/csharp/faq/#one.application.instance

Cheers

Arne Janning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top