私はこの例外について別の記事を見つけましたが、私の場合はどれも例外ではありません。ここ は、ソースコードである:オブジェクト同期メソッドが非同期コードブロックから呼び出されました。 Mutex.Release()の例外
class Program
{
private static Mutex mutex;
private static bool mutexIsLocked = false;
static void Main(string[] args)
{
ICrmService crmService =
new ArmenianSoftware.Crm.Common.CrmServiceWrapper(GetCrmService("Armsoft", "crmserver"));
//Lock mutex for concurrent access to workflow
mutex = new Mutex(true, "ArmenianSoftware.Crm.Common.FilterCtiCallLogActivity");
mutexIsLocked = true;
//Create object for updating filtered cti call log
ArmenianSoftware.Crm.Common.FilterCtiCallLog filterCtiCallLog =
new ArmenianSoftware.Crm.Common.FilterCtiCallLog(crmService);
//Bind events
filterCtiCallLog.CtiCallsRetrieved += new EventHandler<ArmenianSoftware.Crm.Common.CtiCallsRetrievedEventArgs>(filterCtiCallLog_CtiCallsRetrieved);
//Execute filter
try
{
filterCtiCallLog.CreateFilteredCtiCallLogSync();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mutexIsLocked)
{
mutexIsLocked = false;
mutex.ReleaseMutex();
}
}
}
static void filterCtiCallLog_CtiCallsRetrieved(object sender,
ArmenianSoftware.Crm.Common.CtiCallsRetrievedEventArgs e)
{
tryasasas
{
if (mutexIsLocked)
{
mutexIsLocked = false;
mutex.ReleaseMutex();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
filterCtiCallLog.CreateFilteredCtiCallLogSync();
機能は、サーバーへのリクエストを実行し、CtiCallsRetrieve
イベントそのうちの1つは、いくつかのイベントを発生させます。このイベントが発生すると、ミューテックスを解放する必要があります。しかし、mutex.Release()関数の例外を呼び出すと例外がスローされます。 CreateFilteredCtiCallLogSync
は同期して動作します。何が問題ですか?
あなたが正しいです - コールバックを設定するスレッドがメインスレッドであることを忘れました:( –
AutoResetEventは素晴らしいです!ありがとうHans。 –