How can I prevent the opening of two windows in C #?
- fourIf you just read the question stupidly, then the majority will understand it like this: How not to allow a person to open more than one window of the VS C # 2010 program. What exactly is the visual studio, and not the program written in visual studio. Maybe you meant just a program written on a visual studio? And maybe not a second window, but a multiple launch of one application? If so, here's a good article on this topic. Skillcoding.com/Default.aspx?id=89 - Vladyslav Matviienko
|
3 answers
Based on mutex
string id = "GUID..."; using (NamedMutex nm = new NamedMutex(false, id)) { if (!nm.WaitOne(0, false)) { return; } // ... }
Or using the Microsoft.VisualBasic.ApplicationServices namespace - there are special features for this.
- Yeah thanks! - Angus123
|
The programmer has to control himself in which cases which windows will be active / inactive or hidden, for this there are appropriate methods of something like SetVisible and SetActive.
|
If you need to display no more than one dialog box of a certain type in MDI, then you can use the Singelton pattern in the form class. If you need to run no more than one instance of your program, then use Mutex.
- If you need to run just one instance of your program, use Mutex. That's it, and where should it be used? - Angus123
- At the very beginning of the program, check whether there is a busy mutex in the system, if it is, then finish the program, otherwise create a mutex and occupy it - Pavel S. Zaitsau
|