Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

Single instance application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alok - 22 Jun 2006 03:22 GMT
I am programming in c# and making a windows forms application.
I don't want to make a windows service.
How can I make sure that only once instance of the application runs ?
If you try to run another instance , it should bring the running instance to
the foreground and exit itself.

The second thing that I want to achieve is as follows :
This UI will be scheduled to run at system startup. when I login , I can see
the
process running but with no UI, meaning its running in the background.
Now I try running another instance, this should bring the first instance to
the foreground.

Is it actually possible to bring the instance which does not show a UI to
bring to the foreground and show the window ?
I have tried ShowWindow and SetForeGroundWindow but with no success.

I really appreciate any help here.

Thanks,
Alok
Joris Zwaenepoel - 22 Jun 2006 06:59 GMT
Alok,

If you really want the application to start at system startup, before you
log on, then I think a windows service is the way to go.  Why don't you want
to do that?  You could create a UI that communicates with the service (via
remoting) and start that UI when you log on.

Joris

> I am programming in c# and making a windows forms application.
> I don't want to make a windows service.
[quoted text clipped - 17 lines]
> Thanks,
> Alok
Sericinus hunter - 22 Jun 2006 13:18 GMT
> I am programming in c# and making a windows forms application.
> I don't want to make a windows service.
> How can I make sure that only once instance of the application runs ?
> If you try to run another instance , it should bring the running instance to
> the foreground and exit itself.

   Try to play around with this code:

public static void Main()
{
    Process pr = Process.GetCurrentProcess();
    string appName = pr.ProcessName;
    Process[] runningProcesses = Process.GetProcessesByName(appName);

    // at this point we either have one or two processes in the array;
    // if one, then we just started, if two, this is the second instance
    // and we need to activate the first one and exit

    if(runningProcesses.Length == 1)
        Application.Run(new MainWindow());
    else
    {
        IntPtr hwnd = IntPtr.Zero;

        if(!runningProcesses[0].MainWindowHandle.Equals(hwnd))
            handle = runningProcesses[0].MainWindowHandle;
        else if(!runningProcesses[1].MainWindowHandle.Equals(hwnd))
            handle = runningProcesses[1].MainWindowHandle;

        Win32.ShowWindowAsync(hwnd, 2); // SW_MINIMIZE
        Win32.ShowWindowAsync(hwnd, 9); // SW_RESTORE
    }
}
Marc Gravell - 22 Jun 2006 13:44 GMT
This doesn't work so well if you are running in IEExec or a similar host;

A better approach is a Mutex:

http://www.yoda.arachsys.com/csharp/faq/#one.application.instance

Marc
Stoitcho Goutsev (100) - 22 Jun 2006 15:21 GMT
Alok,

Usually in order to find out if an instance is already running programmer
use mutexes. This question has been asked a lot. Try to google for an
answer; you'll find sulution and code snippets how to do that.

Signature

HTH
Stoitcho Goutsev (100)

>I am programming in c# and making a windows forms application.
> I don't want to make a windows service.
[quoted text clipped - 20 lines]
> Thanks,
> Alok
Jim Wooley - 22 Jun 2006 15:30 GMT
You could import the VB 2005 namespaces and use the built-in functionality.
I have an article on the topic at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx.
It is in VB, but the download includes a number of links to samples in VB
and C#.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

> I am programming in c# and making a windows forms application.
> I don't want to make a windows service.
[quoted text clipped - 18 lines]
> Thanks,
> Alo
Mini-Tools Timm - 27 Jun 2006 15:42 GMT
> I am programming in c# and making a windows forms application.
> I don't want to make a windows service.
> How can I make sure that only once instance of the application runs ?
> If you try to run another instance , it should bring the running instance to
> the foreground and exit itself.

Hi Alok,

We offer a .NET component FREE for non-commercial use which will do this for
you:

http://www.mini-tools.com/goto/comm

> The second thing that I want to achieve is as follows :
> This UI will be scheduled to run at system startup. when I login , I can see
[quoted text clipped - 5 lines]
> bring to the foreground and show the window ?
> I have tried ShowWindow and SetForeGroundWindow but with no success.

Yes.  Try this:

    SetForegroundWindow( hWnd );
    SetActiveWindow( hWnd );

You will need these definitions:

    using System.Runtime.InteropServices;
    [DllImport("user32.dll")]
    static private extern bool SetForegroundWindow( IntPtr hWnd );
    [DllImport("user32.dll")]
    static private extern IntPtr SetActiveWindow(IntPtr hWnd);

Signature

Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.