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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -