Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi
In my Program.cs the code is :
C#
bool createdNew;
m_Mutex = new Mutex(true, "App", out createdNew);
if (createdNew)
     Application.Run(new StartForm());
else
     MessageBox.Show("The application is already running.", Application.ProductName,
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


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


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
Posted
Updated 18-Dec-14 9:40am
v2
Comments
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?
—SA
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.

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.

—SA
 
Share this answer
 
Comments
Maciej Los 18-Dec-14 17:22pm    
Sounds... great!
+5!
Sergey Alexandrovich Kryukov 18-Dec-14 17:51pm    
Thank you, Maciej.
—SA
[no name] 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?
—SA
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900