Click here to Skip to main content
14,329,910 members
Rate this:
Please Sign up or sign in to vote.
See more:
In my Program.cs the code is :
bool createdNew;
m_Mutex = new Mutex(true, "App", out createdNew);
if (createdNew)
     Application.Run(new StartForm());
     MessageBox.Show("The application is already running.", Application.ProductName,
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

In my StartForm.cs after InitializeComponent the code is :
Form1 f1 = new Form1();
f1.Visible = false;

My requirement here is :
when I double the application exe when an instance is already running, instead of just displaying "Application is running" I want to check if Form1 is already open.
1. If Form1 is already open do nothing.
2. If Form1 not open showdialog Form1 from StartForm.cs

Please note that StartForm always stays in systray

Thanks in advance
Updated 18-Dec-14 9:40am
Sergey Alexandrovich Kryukov 18-Dec-14 16:17pm
Not a question. There is no such concept as "sub form". What are you trying to achieve? Why using that mutex? Do you want to use it as the indication of first application instance running? What's the problem?
BillWoodruff 19-Dec-14 4:10am
Please clarify: do you want the Application to terminate when all Forms are closed ?

It is possible to have a WinForms app where all Forms can be closed, and the Application is still running: of course, you must have way to interact with the Application when all the Forms are closed: that is usually done by having a SystemTray Notification Icon.
Rate this:
Please Sign up or sign in to vote.

Solution 1

If you are trying to implement single-instance application behavior, my past answers can help you:
Enter multiple commands to only one process instance[^],
Custom Windows right-click command launching multiple instances[^].

Your follow-up questions will be welcome.

Maciej Los 18-Dec-14 17:22pm
Sounds... great!
Sergey Alexandrovich Kryukov 18-Dec-14 17:51pm
Thank you, Maciej.
0x01AA 18-Dec-14 17:41pm
Yep also my 5. But also the boring "Bruno" question again "how to solve this" over session boundaries.
Sergey Alexandrovich Kryukov 18-Dec-14 17:52pm
Thank you, Bruno.
So, what's up with session boundaries? Could you explain the problem?
Rate this:
Please Sign up or sign in to vote.

Solution 2

First, the implication (that I read) in your post that there could be your App running without a Form instance created (so the "Message Pump" of the Application does not depend upon a Form) suggests a Windows Service might be the right choice of App structure for you. Just ignore this comment if I incorrectly interpret your post.

Second, the question of how to make a single-instance WinForms App is one many people have addressed:

1. Scott Hanselman presented a solution in 2008 using the Visual Basic Library in C#: [^].

2. To use the Mutex ... which is, evidently, on your mind ... see the CodeProject article: "Single Instance Form Application in C#" December 2013: [^].

By far the best example, and discussion, of the use of Mutex for a single-instance app I have seen is in Mark Michaelis' "Essential C# 4.0," Chapter 19.8. The sample code for that book can be downloaded (.pdf) here: [^], and you will find his sample code in the Chapter 19 folder.

3. Chris Sells in his 2006 book on C# WinForms (Chapter 14) covers several interesting alternative ways to run a WinForms Application: [^].

Chapter 14 is available as a free download (.pdf) here: [^].

Note that this chapter does not cover creating a single-instance application; for that, Sells, like Hanselman, uses the Visual Basic Library, and that is covered in another chapter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100