1

I need to share an IsolatedStorage file between foreground (unit tests) and the background agent.

I have code similar to the following:

Foreground:

private Mutex _mut = new Mutex(false, “backgroundShare”);


private Task WriteToFile()
{
 ……….
    _mut.WaitOne(100);

    try{
       // open and write to the file “sharedFile.del” here, then close and dispose the writer and the stream…
    }
    finally
    {
         _mut.ReleaseMutex();
    }
 }

Background Task:

private Mutex _mut = new Mutex(true, “backgroundShare”);


private void ReadFile()
{
    …………………….
    _mut.WaitOne(15);

    try{
         //folder.GetFileAsync(“sharedFile.del”); 
          ……
    }
    finally 
    {
        _mut.ReleaseMutex();
    }
}

This code is as per the StackOverflow posts I found for the WP7.x. Is this code valid for WP8?

Also, when I run my unit tests (foreground app), it sometimes gives me this error:

System.AggregateException: One or more errors occurred. ---> System.Exception: Object synchronization method was called from an unsynchronized block of code. Result StackTrace:
at System.Threading.Mutex.ReleaseMutex()

While sometimes it passes but then the background task does not get the shared file. The GetFileAsync call causes the background agent to kill itself (atleast that Is what I am seeing in the debugger). Some other times, it works fine!

The unit test structure is as follows:

// write to the shared file here WriteToFile(); …… ScheduledActionService.LaunchForTest(MaintenanceTaskName, TimeSpan.FromMilliseconds(10)); …….. //check if Background task actually read the file and did the right thing here…

What do you think is wrong in this? Why is it not working correctly at all times? Any advice is highly appreciated! Thanks much!

mico
  • 12,730
  • 12
  • 59
  • 99
DSK
  • 11
  • 2

0 Answers0