Window Handler issue

  • Thread starter Thread starter Sriman
  • Start date Start date
S

Sriman

We are trying to bring the window on top if its already running(Restore). The
code works fine 90% of the time but sometimes it does not. The reason it does
not work at times is coz the window handle is not returned. need confirmation
on this.

Any help would be greatly appreciated.

Code as follows:
==================================================
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[STAThread]
static void Main()
{
try
{
// get the name of our process
string proc = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(proc);
Process.GetCurrentProcess().StartInfo.WindowStyle =
ProcessWindowStyle.Maximized;
// if there is more than one process...
if (processes.Length > 1)
{
// get the window handle
IntPtr hWnd = processes[0].MainWindowHandle;
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, 9);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
return;
}
else
{
//Added for checking registry entry proper or not
if (CheckForInstallation())
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLogin());
}
else
{
MessageBox.Show(AppGlobalization.GetText("InstallError"),
AppGlobalization.GetText("Error"), MessageBoxButtons.OK,
MessageBoxIcon.Error);
Application.Exit();
}
}
}
catch (Exception ex)
{
General.ShowInformation("Application Operations Failed -Main(Program.cs): "
+ ex.Message + " " + "[" + ex.StackTrace + "]", false);
}
}
 
Sriman said:
We are trying to bring the window on top if its already
running(Restore). The code works fine 90% of the time but sometimes

It works 0% of the time. ;) This is a VB.Net group.


Armin
 
Back
Top