D
D
deadspace122021-10-16 08:22:42
C++ / C#
deadspace12, 2021-10-16 08:22:42

How to get the handle of the program being launched?

How to get the handle of the program being launched? By assignment, I'm trying to create a mutex and in case of successful synchronization, it will write data to a file. After starting, it swears at the mutex opening line and writes that this descriptor does not exist, please help with the problem. Here is the code that I implemented:

static void Main(string[] args)
        {
            IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
            Console.WriteLine(handle);
            Mutex mut = Mutex.OpenExisting(handle.ToString());
            Random md = new Random();
            for (int i = 0; i < 100000; i++)
            {
                mut.WaitOne();
                StreamWriter sw = new StreamWriter(@"F:\CПО\6лб\ConsoleApp7\1.txt", true);
                int j = md.Next(100000);
                sw.WriteLine(j);
                Console.WriteLine("Записано {0}", j);
                sw.Close();
                mut.ReleaseMutex();
                Thread.Sleep(100);
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-10-16
@deadspace12

Not very familiar with Mutex, but something tells me from the first example that came across that you need to use new Mutex(true, handle.ToString()) instead of Mutex.OpenExisting(handle.ToString()) .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question