0
我有以下方法,該方法由我的ReportGenerator類的不同實例調用。 ReportGenerator類啓動一個單一任務,該任務使用包含此方法的類的接口訪問以下方法。鎖定方法中的任務,在類的不同實例上調用
public IRapportBestilling GetNextReportOrderAndLock(DateTime nextTimeoutValue, string correlationId, Func<EstimatedReportSize?, int, bool> getPermission, int taskId)
{
this.ValidateReportOrderQueueTimeout(nextTimeoutValue, correlationId);
IRapportBestillingKoe reportOrderQueue;
try
{
using (var scope = new QueryEngineSessionScope())
{
--> Lock here bool allowLargeReports = getPermission.Invoke(EstimatedReportSize.RequestForLarge, taskId);
reportOrderQueue = this.rapportBestillingKoeRepository.GetNextReportOrderQueueItem(scope, correlationId, allowLargeReports, taskId);
reportOrderQueue.Laast = true;
reportOrderQueue.Timeout = nextTimeoutValue;
this.rapportBestillingKoeRepository.Save(reportOrderQueue, scope, correlationId);
scope.Complete();
--> Release lock getPermission.Invoke(reportOrderQueue.EstimeretRapportStoerelse, taskId);
var rep = this.rapportBestillingRepository.GetDomainObjectById(reportOrderQueue.RapportBestillingId, scope, correlationId);
rep.StorRapport = (reportOrderQueue.EstimeretRapportStoerelse == EstimatedReportSize.Large);
return rep;
}
}
}
我需要只允許一個任務執行上面顯示的方法中的代碼塊。我已經使用Interlocked以及Monitor類來處理這個問題,但是這不起作用,因爲這個方法在我的類的不同實例上被調用。有沒有辦法來解決這個問題?
根據您的使用情況,您可以創建一個靜態對象並對其進行鎖定。 – Rob