2012-06-26 153 views
2

它是爲使用相同的負荷運行來加載多個實體可接受的實踐,然後將其結合到異步地單個事件處理程序?我這樣做是因爲我使用了一個繁忙的指標,而且我們不知道哪個實體會更快地返回。的Silverlight 4個+ RIA服務加載

或者我應該將加載到多個負載操作/事件處理程序對分開?

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

回答

1

LoadOperation調用getResult;

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