2012-03-04 2 views
3

편집 : VS2008, .NET 3.5을 사용하고 있습니다.스레딩과 함께 DataView에서 RowFilter를 올바르게 사용 했습니까?

데이터 뷰에 채워지고 CollectionView로 전달되는 DataTable이 있습니다.

DataTable newLeadTable = new DataTable(); 
    myConnection.Open(); 
    dbAdpater.Fill(newLeadTable); 
    this.LeadDataView = new DataView(newLeadTable); 
    this.LeadsCollectionView = 
    CollectionViewSource.GetDefaultView(this.LeadDataView); 

그 다음 데이터를 표시하는 데 사용하는 데이터 그리드보기에 필터 텍스트를 입력하는 사용자 후 this.LeadsCollectionView

에 결합되고, 뷰 모델은 DataView를에 필터 문자열을 설정하기 위해이 방법을 실행한다 :

private void SetLeadListFilter(string LeadFilterStr) 
{ 
    this.LeadDataView.RowFilter = filterString; 
} 

정상적으로 작동하며 DataGrid가 적절한 필터링 된 DataRow를 보여줍니다.

그런데 바쁜 표시기를 추가하여 UI 체험을하고 싶습니다. 그래서 나는 스레드에 위의 코드를 넣어 :

this.IsBusy = true; 
Thread filterDataThread= new Thread(new ThreadStart(() => 
{   
    this.LeadDataView.RowFilter = filterString; 
    this.IsBusy = false; 
})); 
filterDataThread.Start(); 

지금은 이상하다, 나는 코드가 실행 된 필터가 잘 살고되는 것을 볼 수 있습니다. 그러나 DataGrid는 행을 필터링하지 않습니다!

그래서 지금은, 방법을 수정할 다시 CollectionView에 DataView를 재 할당 :

this.IsBusy = true; 
Thread filterDataThread= new Thread(new ThreadStart(() => 
{   
    this.LeadDataView.RowFilter = filterString; 
    this.LeadsCollectionView = CollectionViewSource.GetDefaultView(this.LeadDataView); //Added this 
    this.IsBusy = false; 
})); 
filterDataThread.Start(); 

는 이제 작동! 데이터가 DataGrid에서 올바르게 필터링되고 있습니다!

그렇다면 스레딩을 사용할 때 왜 이런 일이 발생합니까? 이것이 스레딩에서 DataFilter를 사용하는 적절한 방법입니까? 내가 이해 한대로

답변

0

당신이 당신의 데이터 그리드의 데이터 소스를 얻을 수

CollectionViewSource.GetDefaultView(this.LeadDataView); 

를 사용, 확실히 말할 수는 없지만. 대답은이 가정에 기초합니다. MainThreadfilterDataThreadMainThread 사이에 경쟁이있을 것 -

this.IsBusy = true; 
Thread filterDataThread= new Thread(new ThreadStart(() => 
{   
    this.LeadDataView.RowFilter = filterString; 
    this.IsBusy = false; 
})); 
filterDataThread.Start(); 
this.LeadsCollectionView = 
    CollectionViewSource.GetDefaultView(this.LeadDataView); 

다음 GetDefaultView와 코드가 아마 전에 스레드 작업을 를 실행합니다 :이처럼 filterDataThread 외부에서이 방법을 살았어합니다 그래서 만약
이기고, DataGrid은 데이터를 필터링하지 않습니다. 당신이 제공 한 코드를 사용할 경우

그러나 :

this.IsBusy = true; 
Thread filterDataThread = new Thread(new ThreadStart(() => 
{   
    this.LeadDataView.RowFilter = filterString; 
    this.LeadsCollectionView = 
     CollectionViewSource.GetDefaultView(this.LeadDataView); //Added this 
    this.IsBusy = false; 
})); 
filterDataThread.Start(); 

필터링은 적절한시기에 시작됩니다.
대답은 다음과 같습니다.
예, 필터링 권한이 있습니다. 그러나 백그라운드 스레드에서 작업을 수행하는 동안 오류를 처리하는 코드를 추가하여 응용 프로그램이 영원히 Busy이되지 않도록해야합니다.

또한 filterString에 대한 안전한 액세스를 확인해야합니다. 필터링을 위해 두 개 이상의 스레드를 시작하는 경우 예측할 수없는 결과가있는 다른 경주가 있습니다.

관련 문제