C# 将从EF上下文检索到的两个POCO抛出到新线程任务中

C# 将从EF上下文检索到的两个POCO抛出到新线程任务中,c#,wpf,multithreading,entity-framework,poco,C#,Wpf,Multithreading,Entity Framework,Poco,如果我在主线程上创建一个上下文来检索相同类型的两个POCO,并将这两个POCO扔到一个新线程中,以便映射属性,那么这样做安全吗 public class ViewModelContainer { private readonly IMappingService mappingService; public ViewModelContainer(IMappingService mappingService) { this.mappingService = m

如果我在主线程上创建一个上下文来检索相同类型的两个POCO,并将这两个POCO扔到一个新线程中,以便映射属性,那么这样做安全吗

public class ViewModelContainer
{
    private readonly IMappingService mappingService;

    public ViewModelContainer(IMappingService mappingService)
    {
       this.mappingService = mappingService;
       this.mainContext = GetNewContext();
       List<MyPoco> allMyPocos = mainContext.MyPocos.ToList();
    }

    public void MapBothPocos()
    {
       MyPoco sourcePoco = allMyPocos.FirstOrDefault(x => x.IsSourcePoco);
       var targetPocos = allMyPocos.Where(x => x.IsTargetPoco);

      Task.Factory.StartNew(() =>
      {
         foreach (var target in targetPocos)             
             this.mappingService.LinkSourceToTarget(sourcePoco, target);
      });
        ^A LOT of mapping TO DO HERE!!!
    }
}

public class MappingService : IMappingService
{
   public void LinkSourceToTarget(MyPoco source, MyPoco target)
   {  
         //THERES A FAIR AMOUNT OF LOGIC TO PREPARE THE SOURCE AND TARGET BEFORE THEY CAN BE MAPPED
         //Prepare...
         //some more prepare...

         //ok now lets map
         Mapper.Map(sourcePoco, targetPoco); //AutoMapper

   }

   //PLUS A LOT OF OTHER LOGIC WILL BE IN HERE. 
}

我主要想这样做,以便我可以把一个准确的加载进度条在屏幕上。如果这样做是错误的,有没有办法让我在应用程序的屏幕上显示一个IsBusy指示灯

Mapper.Map是否花费了那么多时间,以至于您只想使用线程池线程来映射POCO?这一定是一个复杂的POCO,需要花费这么多时间!我有很多POCO为我做这件事。让我们假设它确实需要一段时间while@TMan然后,您应该将整个内容包装在一个任务中,而不是单个Mapper.Map函数中。如果您的代码示例与您的RL案例不匹配,那么您的代码示例将不会真正匹配。你应该用一个能反映问题的代码来编辑你的代码,这样我们才能提供一个有价值的答案。