C#: how to detect window application is running and not launch the same application again?
发布时间:2026-09-11 | 浏览:1
Access to this page requires authorization. You can try signing in or changing directories .
Access to this page requires authorization. You can try changing directories .
Thursday, June 28, 2012 11:59 PM
We have WinForm application (.NET Framework 3.5). Sometime the WinForm application is running in back by calling Hide();
If user click on the ~.exe again, we need to know if the WinForm application is already running, if so,
1) it should not launch the application again.
2) and it should show the running WinForm from Hide() to Show().
How to achieve that? Thx!
All replies (13)
Saturday, June 30, 2012 8:52 AM ✅Answered | 1 vote
Single instance application implementation is been clearly described here by Scott and it's really easy to follow.
Please click "Mark As Answer" if it serve the purpose.\n Regards, Nairs
Saturday, June 30, 2012 5:53 PM ✅Answered
I tested Scott Hanselman 's code adapted to show a hidden MainForm and it worked nicely.
Here is the modified code that will solve your second question:
In Form1.cs, you can remove the OnLoad override method and replace LoadFile with ShowForm
Note that you need to add the Microsoft.VisualBasic (.NET tab) reference to your project.
Dan Randolph - My Code Samples List
Friday, June 29, 2012 2:59 AM | 1 vote
You can use Mutex for this purpose.
For more info visit this nice article,
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
Friday, June 29, 2012 4:55 AM
If this is helpful { Please Mark as Answered } else if it is not helpful { Un-Mark as Answered }
Best Regards Huan Li, Code Blog: Small is New Big In C#
Friday, June 29, 2012 4:58 AM
Hello JJChen:-)
In VB.NET, just right click ur proj and choose "properties", and check this, plzzzzzz……
QQ我: 下载MSDN桌面工具(Vista,Win7) 我的博客园 慈善点击,点击此处
Friday, June 29, 2012 5:28 AM
This one doesn't work if I change the executable name and run it.
Please mark this post as answer if it solved your problem. Happy Programming!
Friday, June 29, 2012 5:28 AM
2) and it should show the running WinForm from Hide() to Show().
How to achieve that? Thx!
I tried this with a Win32 trick, but I could not get the form to show. The same function worked to restore the window state from minimized to normal though.
If no one posts an easy solution for (2), I will post a code sample on how to do it using a signal via a named-pipe IO operation. I have this working, but it is too much code to post here.
Dan Randolph - My Code Samples List
Friday, June 29, 2012 5:44 PM
Thanks for providing info.
we found the application Guid from application AssemblyInfo.cs
However the above code you provided does not work.
the link you provide includes working code as following:
static class Program { static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}"); [STAThread] static void Main() { if(mutex.WaitOne(TimeSpan.Zero, true)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); mutex.ReleaseMutex(); } else { MessageBox.Show("only one instance at a time"); } } }
Friday, June 29, 2012 6:19 PM
Please provide your code if you could. Thx!
Friday, June 29, 2012 7:53 PM
You can use Mutex for this purpose.
For more info visit this nice article,
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
It's worth noting that a solution along these lines will only work with benign users. If it's important for say security reasons that there not be two instances open this won't be sufficient as someone could decompile your code, and then re-compile it without that check. If the user is only shooting themselves in the foot by opening multiple instances, or the possible gains for them just aren't worth this kind of effort, then it's a fine solution.
Friday, May 23, 2014 7:02 AM
There are times mutex is not working in some areas. Like using in console application. So I tried using WMI Query.
For more info you may email me here: ocbina_jay@hotmail.com, ocbina_jay@yahool.com
htt://israelocbina.blogspot.com
Thursday, May 3, 2018 5:50 AM
This will not work properly if user change the the (.exe) file name
Thursday, May 3, 2018 5:54 AM
This is the perfect solution
as if we depend on process name , user can simply change file name then process name will be changed
Additional resources
Last updated on 2012-06-28