Synchronization WP8中的后台代理同步
我需要在前台(单元测试)和后台代理之间共享一个隔离的存储文件 我有类似以下代码: 前景:Synchronization WP8中的后台代理同步,synchronization,background-agent,Synchronization,Background Agent,我需要在前台(单元测试)和后台代理之间共享一个隔离的存储文件 我有类似以下代码: 前景: 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 di
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.Threading.Mutex.ReleaseMutex()中 虽然有时它会通过,但后台任务无法获取共享文件。GetFileAsync调用会导致后台代理自行终止(至少这是我在调试器中看到的)。有时,它工作得很好 单元测试结构如下: //在此处写入共享文件 WriteToFile(); …… ScheduledActionService.LaunchForTest(MaintenanceTaskName,TimeSpan.FromMillicles(10)); …….. //检查后台任务是否真的读取了文件并在这里做了正确的事情 你认为这有什么不对?为什么它不能一直正常工作?非常感谢您的建议!多谢