WP8中的后台代理同步

时间:2013-04-09 18:29:36

标签: synchronization background-agent

我需要在前台(单元测试)和后台代理之间共享一个IsolatedStorage文件。

我的代码类似于以下内容:

前景:

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();
    }
 }

后台任务:

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


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

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

此代码符合我为WP7.x找到的StackOverflow帖子。这段代码对WP8有效吗?

另外,当我运行我的单元测试(前台应用程序)时,它有时会给我这个错误:

System.AggregateException:发生了一个或多个错误。 ---> System.Exception:从未同步的代码块调用对象同步方法。 结果StackTrace:
在System.Threading.Mutex.ReleaseMutex()

虽然有时会通过,但后台任务却没有获取共享文件。 GetFileAsync调用导致后台代理自行终止(至少是我在调试器中看到的)。其他一些时候,它工作正常!

单元测试结构如下:

//在此处写入共享文件 将writeToFile(); ...... ScheduledActionService.LaunchForTest(MaintenanceTaskName,TimeSpan.FromMilliseconds(10)); ...... .. //检查后台任务是否实际读取了文件,并在此处做了正确的事......

你认为这有什么不对?为什么它在任何时候都无法正常工作?任何建议都非常感谢!非常感谢!

0 个答案:

没有答案