2

使用相同的加载操作来加载多个实体然后将其异步绑定到单个事件处理程序是否可以接受?我这样做是因为我使用了一个繁忙的指示器,而且我们真的不知道哪个实体会更快地返回。

还是应该将负载分成多个负载操作/事件处理程序对?

  LoadOperation GetResult;

    private void LoadinDatagrid()
    {   
        IsBusy = true;
        GetResult = DomainContext.Current.Load(GetSomething1Query());
        GetResult = DomainContext.Current.Load(GetSomething2Query());
        GetResult = DomainContext.Current.Load(GetSomething3Query());         

        GetResult.Completed += new EventHandler(GetResult_Completed);                     
    }

    void GetResult_Completed(object sender, EventArgs e)
    {

        GetSomething1 = DomainContext.Current.Something1;
        GetSomething2 = DomainContext.Current.Something2;
        GetSomething3 = DomainContext.Current.Something3;
        GetResult.Completed -= new EventHandler(GetResult_Completed);
        IsBusy = false;     
    }
4

2 回答 2

1

加载操作获取结果;

private void LoadinDatagrid()
{   
    IsBusy = true;

    GetResult.Completed += new EventHandler(GetResult_Completed);       
    GetResult = DomainContext.Current.Load(GetSomething1Query());
    GetResult = DomainContext.Current.Load(GetSomething2Query());
    GetResult = DomainContext.Current.Load(GetSomething3Query());         

}

void GetResult_Completed(object sender, EventArgs e)
{
   if(e.result!=null){
    if(e.result.x=="GetSomething1")
    GetSomething1 = DomainContext.Current.Something1;
    else if(e.result.x=="GetSomething2")
    GetSomething2 = DomainContext.Current.Something2;
    else if(e.result.x=="GetSomething3")
    GetSomething3 = DomainContext.Current.Something3;
    //
    //Because when returned 1st data then stop eventhandler.
    //GetResult.Completed -= new EventHandler(GetResult_Completed);
    //
   }
    IsBusy = false;     
}
于 2012-10-29T09:32:12.027 回答
0

尝试以下操作:

private int loadCounter;

private void LoadinDatagrid()
{   
    loadCounter = 3;
    IsBusy = true;
    GetResult = DomainContext.Current.Load(GetSomething1Query(), LoadCompleted);
    GetResult = DomainContext.Current.Load(GetSomething2Query(), LoadCompleted);
    GetResult = DomainContext.Current.Load(GetSomething3Query(), LoadCompleted);         
}

private void LoadCompleted(LoadOperation result)
{
    Interlocked.Decrement(loadCounter); // Thread save decrementing
    if(loadCounter == 0) // All queries have been loaded
    {
        IsBusy = false;
    }
}
于 2012-10-29T09:55:28.363 回答