2012-10-24 2 views
2

데이터베이스로 업데이트하는 Windows 프로그램이 있습니다. 그것은 진도를 보여주고 싶습니다 2 루프가 있습니다. 첫 번째 루프는 고객의 목록을 가져옵니다 두 번째는 그 고객에 대한 위치의 목록을 가져옵니다 :2 개의 다른 루프에서 다중 진행 막대를 업데이트하십시오.

DataTable dtCustomers = GetAllCustomers(); 

foreach(DataRow customer in dtCustomers.Rows) 
{ 
    //update Customer Progressbar... 

    //do some updating here... 

    DataTable dtLocations = GetAllLocations(customer); 

    foreach(DataRow location in dtLocations.Rows) 
    { 
     //do some updating here... 

     //update Location Progressbar... 
    } 

    //reset Location Progressbar... 
} 

그래서 내가하고 싶은 것을 각 루프의 시각적 진행 막대 (PB)를 표시합니다. 고객 pb는 처리 된 고객마다 증가하므로 위치 pb도 마찬가지입니다 ... 유일한 차이점은 위치에 따라 업데이트하는 데 더 길거나 짧을 수 있기 때문에 위치 pb가 각 위치 이후에 재설정된다는 것입니다.

저는 1 명의 배경 작업자로 시작하여 고객에게 잘 업데이트 할 수있었습니다. 나는 "시작"버튼에 다음 코드를 넣어 :

private void buttonStart_Click(object sender, EventArgs e) 
{ 
    workerCustomers.RunWorkerAsync(); 
} 

과 workerCustomer의 DoWork() 이벤트에

, 나는 2 개 개의 루프를 넣어. 나는 그것이 "크로스 스레드 참조"오류를 줄 것이기 때문에 위치 pb가 업데이트되지 않는다는 것을 안다. 그렇다면 내가 원하는 것을 어떻게 할 수 있습니까? 나는 심지어 양식에 2 bg의 근로자를 배치하고 다른 근로자로부터 하나를 불러 내기 위해 노력했지만, 다시 말해서 첫 번째 근로자가 바쁜 것을 나타내는 또 다른 오류가있었습니다.

workerCustomers.ReportProgress(percentage1, percentage2); 

을 그리고 ProgressChanged 이벤트 처리기에서 당신은 둘 ProgressBar를를 업데이트 할 수 있습니다 : 당신은 진행 상황을보고 할 때

답변

4

초 매개 변수 (ReportProgress 방법 (고화질)를 참조)는 추가 userState 개체를 전달할 수 있습니다

void workerCustomers_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    progressBar1.Value = e.ProgressPercentage; // Customers 
    progressBar2.Value = (int)e.UserState; // Locations 
} 

UPDATE를 : 귀하의 케이스에서 사용하는 방법

DataTable dtCustomers = GetAllCustomers(); 
int customerIndex = 0; 

foreach(DataRow customer in dtCustomers.Rows) 
{ 
    //do some updating here... 
    int customerPercentage = ++customerIndex * 100/dtCustomers.Rows.Count; 
    workerCustomers.ReportProgress(customerPercentage, 0); 

    int locationIndex = 0; 
    DataTable dtLocations = GetAllLocations(customer); 

    foreach(DataRow location in dtLocations.Rows) 
    { 
     //do some updating here... 
     int locationPecentage = ++locationIndex * 100/dtLocations.Rows.Count; 
     workerCustomers.ReportProgress(customerPercentage, locationPecentage); 
    } 

    workerCustomers.ReportProgress(customerPercentage, 0); 
} 
+0

감사합니다 f 또는이 있지만 여전히 어떤 이유로 2 번째 진행률 표시 줄을 업데이트하지 않습니다. 그 코드가 도움이되는지 확인하기 위해 기본 코드로 코드를 업데이트했습니다 (위의 모든 코드를 잘라내 었습니다). – Robert

+1

@ 로버트 질문을 변경했기 때문에 질문의 업데이트를 롤백했습니다. backgroundWorker에서 두 개의 값을 전달하는 방법을 제공했습니다. 다른 문제가있는 경우 다른 질문을 만들어야합니다. –

관련 문제