c# - Windows Phone Periodic Task Synchronization Async Methods -
i attempting make use of periodic background task on windows phone 8 app. want use xml file serialize state information between foreground app , background task.
i read should use mutex synchronize access file. however, run issues using it, because need call await in method use mutex in (to read , write data file). causes app lock when call mutex.release, since being released on different thread. ideas how handle this?
public async task writestate(backgroundtaskstate state) { using (var m = new mutex(false, backgroundtaskstate.mutexname)) { try { m.waitone(); using (var stream = await getfilestreamforwriting()) { xmlserializer xmlserializer = new xmlserializer(typeof(backgroundtaskstate)); xmlserializer.serialize(stream, state); await stream.flushasync(); } } catch (exception) { } { m.releasemutex(); } } }
a mutex
must released same thread acquired cannot using await
. can use semaphore
achieve same result, since not bounded ant particular thread.
semaphore.waitone(); await task.delay(2); // async call semaphore.release();
Comments
Post a Comment