Почему эта программа по ошибке? 'Объектный метод синхронизации назвали от несинхронизируемого блока кода'

Используйте ProcessStartInfo класс и присвойте значение WorkingDirectory свойство.

10
задан 13 December 2009 в 13:24
поделиться

2 ответа

Your code seems to have a number of problems. The main thread starts a new thread, then calls update. In update it tries to unlock mutex2, but it hasn't locked mutex2 yet, so this fails with an exception.

Even if this error is fixed, the other thread is equally doomed to failure. It will try to release mutex1 before it has locked it.

What are you trying to do here? Are you confusing Mutex with AutoResetEvent?

Also I'm guessing that these two lines are a copy/paste error because they appear twice:

        mut2.WaitOne(); Console.WriteLine("2W");
        mut1.ReleaseMutex(); Console.WriteLine("1R");
4
ответ дан 3 December 2019 в 14:53
поделиться

It is not a great error message, Windows produces it. What it really means is that you are calling ReleaseMutex on a mutex that you don't own. You'll get past the first exception with

static volatile Mutex mut2 = new Mutex(true);

But then it will die inside the thread when it calls ReleaseMutex on mut1, which it doesn't own. Not sure what you're trying to do, the code doesn't make much sense to me.

22
ответ дан 3 December 2019 в 14:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: