相关疑难解决方法(0)

使用await时Monitor.Exit上的SynchronizationLockException

我正在创建一段代码,从我们拥有的遗留系统中获取网页.为了避免过多的查询,我正在缓存获取的URL.我正在使用Monitor.Enter,Monitor.Exit并仔细检查以避免该请求被发出两次,但在释放锁定时Monitor.Exit,我得到此异常:

System.Threading.SynchronizationLockException was caught
  HResult=-2146233064
  Message=Object synchronization method was called from an unsynchronized block of code.
  Source=MyApp
  StackTrace:
       at MyApp.Data.ExProvider.<OpenFeature>d__0.MoveNext() in c:\Users\me\Documents\Visual Studio 2013\Projects\MyApp\MyApp\Data\ExProvider.cs:line 56
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at MyApp.Data.ExProvider.<GetSupportFor>d__15.MoveNext() in c:\Users\me\Documents\Visual Studio 2013\Projects\MyApp\MyApp\Data\ExProvider.cs:line 71
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

线56是Monitor.Exit.这是执行操作的代码:

private async Task<Stream> OpenReport(String report)
{
    var file = _directory.GetFiles(report+ ".html");
    if (file != null && file.Any()) …
Run Code Online (Sandbox Code Playgroud)

c# multithreading synchronization task-parallel-library async-await

8
推荐指数
2
解决办法
6904
查看次数