Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question