Tôi đã tìm thấy các bài viết khác nhau về ngoại lệ này nhưng không có trường hợp nào trong số đó là trường hợp của tôi. Đây là mã nguồn:Phương thức đồng bộ hóa đối tượng được gọi từ một khối mã không đồng bộ. Ngoại lệ trên 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();
chức năng thực hiện các yêu cầu tới máy chủ, và làm tăng một số sự kiện, một trong số đó là CtiCallsRetrieve
sự kiện. Và tôi cần phải phát hành mutex khi sự kiện này được kích hoạt. Nhưng khi gọi hàm ngoại lệ mutex.Release() được ném. CreateFilteredCtiCallLogSync
hoạt động đồng bộ. Vấn đề là gì?
Bạn nói đúng - Tôi đã bỏ lỡ rằng việc thiết lập chuỗi gọi lại là chủ đề chính: ( –
AutoResetEvent thật tuyệt vời! Cảm ơn Hans. –