我遇到了解Mutex類以及如何在WP8應用程序中正確使用它的問題。我試圖做的是隻允許一個ScheduledAgent或應用程序一次讀/寫文件到存儲。我已經嘗試過這種方式,沒有運氣。任何幫助,將不勝感激。WP8 Mutex - 資源鎖定
ScheduledAgent代碼:
下面代碼中的錯誤,當我調用ReleaseMutex()。當我創建Mutex對象時,此問題已修復:new Mutex(true,「FlightPathData」)。但我永遠不能得到WaitOne的鎖和掛起()
錯誤
對象同步方法從代碼不同步塊調用。
代碼
readonly Mutex mutex = new Mutex(false, "FlightPathData");
protected async override void OnInvoke(ScheduledTask task)
{
List<METAR> savedMetars = null;
var facility = ....;
bool mutexAcquired = mutex.WaitOne();
try
{
if (mutexAcquired)
{
savedMetars = await Data.LoadMetarsAsync(facility.Identifier);
//Thread.Sleep(15000);
}
}
finally
{
if (mutexAcquired)
{
mutex.ReleaseMutex();
}
}
NotifyComplete();
}
ViewModelCode(工程罰款,直到ScheduledAgent ReleaseMutex()失敗)
static readonly Mutex mutex = new Mutex(true, "FlightPathData");
...
mutex.WaitOne();
var savedMetars = await Data.LoadMetarsAsync(this.Facility.Identifier);
mutex.ReleaseMutex();
看起來這一行:savedMetars = await Data.LoadMetarsAsync(facility.Identifier);導致任務進入另一個線程,因爲它是異步的,當它嘗試釋放它不能的Mutex時。我評論說,代碼和一切工作,因爲它應該...非常奇怪。 – Clarke76
@Stehen:你能詳細解釋一下「你不能在異步代碼中使用互斥鎖嗎?」嗎?在我的UWP應用程序中,我需要同步前臺應用程序及其後臺任務(生活在不同的進程中)之間的資源訪問(例如存儲文件)。在我的[測試](http://stackoverflow.com/questions/35843812/how-to-synchronize-resource-access-between-uwp-app-and-its-background-tasks)互斥似乎工作正常(我知道不是證明)。 –