D
D
des1roer2016-06-21 13:47:42
C++ / C#
des1roer, 2016-06-21 13:47:42

C# how to find the number of running instances of a program?

How can I prevent multiple copies of the program from running?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2016-06-21
@petermzg

This is solved by using a named mutex.

D
des1roer, 2016-06-21
@des1roer

static void Main()
        {
            using (var sem = new Semaphore(1, 1, "MyApplication"))
            {
                if (sem.WaitOne(0))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                    MessageBox.Show("Программа уже запущена.");
            }

        }

S
Sergey Vinogradov, 2016-06-30
@genI3

As for me, the most convenient way without semaphores and mutexes is to use the VB library.
An example is here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question