2017-03-29 2 views
0

나는 backgroundworker 문제가 있습니다. - 나는 스레딩에 익숙하지 않으므로 가능한 한 쉽게이 작업을 수행하려고합니다.C# backgroundworker advice

내 주된 문제는 최종 사용자가 .NET 4.0 만 지원하므로 await/async를 사용할 수 없으며 BGW가 내가 사용하는 프레임 워크에 가장 적합하다고 들었습니다.

DataGridview가 채워지는 동안로드하려는 GIF 애니메이션이있는 "기다려주십시오"양식이 있습니다. 어떻게 든 잠깐 기다려주십시오. "잠시만 기다려주십시오."라는 질문을 끝내기 위해 채워 넣었는지 확인해야합니다. 그러나 이것을 달성하는 방법에 조금 갇혀 있습니다.

public void btnSearch_Click(object sender, EventArgs e) 
    { 
     backgroundWorker1.RunWorkerAsync(); 
     Application.DoEvents(); 
     try 
     { 
      this.TestDataTableAdapter.Fill(this.TesteDataData.TestDataTable, txtHotName.Text, ((System.DateTime)(System.Convert.ChangeType(txtDepartFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtDepartTo.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime))))); 
      int RowC = TestDataTableDataGridView.RowCount; 
      if (RowC == 0) 
      { 
       MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand); 
      } 
     } 
     catch (System.Exception exc) 
     { 
      MessageBox.Show 
       (
       "Problem" + 
       exc.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Warning 
       ); 
     } 
     finally 
     { 
      //pleaseWait.Close(); 
     } 

내 DataGridView에 데이터를로드하는 버튼입니다. 그리고 지금까지이

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     pleaseWait.ShowDialog(); 
    } 

finally 인해 크로스 스레드로 작동하지 않습니다 내 DoWork 이벤트입니다 (그래서 현재 주석)하지만 난/루프를 수행 DataGridView에가 작성되어 있는지 확인하기 위해 확인 작업을 할 필요가 완료되면 DoWork을 닫습니다. 또는 어떤 방법을 뛰어 다니면 RunWorkerCompleted이되고 그 대신에 pleaseWait.Close();을 넣을 수 있습니다.

제안 사항을 알려주십시오.

+0

작업이 끝날 때 이벤트를 촬영하는 방법은 무엇입니까? –

+1

'backgroundWorker1.DoWork'가 아니라 메인 ui 스레드에'pleaseWait' 대화 상자를 보여주고'backgroundWorker1'의'RunWorkerCompleted' 이벤트에 숨겨야합니다. 'this.TestDataTableAdapter.Fill' 부분은'backgroundWorker1.DoWork'에 들어가야 만합니다. – Pikoh

답변

1

당신은하지 backgroundWorker1.DoWork에, 메인 UI 스레드에 pleaseWait 대화 상자를 표시하고, backgroundWorker1RunWorkerCompleted 경우에 그것을 숨길해야합니다. 당신은 문제가 있다면이 코드에서, 물론

public void btnSearch_Click(object sender, EventArgs e) 
{ 
    pleaseWait.ShowDialog(); 
    backgroundWorker1.RunWorkerAsync(); 
} 

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    try 
    { 
     this.TestDataTableAdapter.Fill(this.TesteDataData.TestDataTable, txtHotName.Text, ((System.DateTime)(System.Convert.ChangeType(txtDepartFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtDepartTo.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime))))); 

    } 
    catch (System.Exception exc) 
    { 
     //You can't show a messagebox here,as it is not in the UI thread 
    } 
} 

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    pleaseWait.Close(); 
    int RowC = TestDataTableDataGridView.RowCount; 
    if (RowC == 0) 
    { 
      MessageBox.Show(GlobVar.NoResults, "", MessageBoxButtons.OK, MessageBoxIcon.Hand); 
    } 
} 

는 AS : 코드가 더 많거나 적은 다음과 같아야 있도록 this.TestDataTableAdapter.Fill 부분은 backgroundWorker1.DoWork에 가야 하나입니다 TextBoxes에 액세스하려고하고 다른 스레드에서 액세스 할 수 없으므로 TestDataTableAdapter.Fill 코드가 작동하지 않습니다.

몇 가지 해결책이 있습니다. backgroundworker를 호출하기 전에 일부 변수를 사용하여 값을 읽고 TextBoxes 대신이 변수에 액세스 할 수 있습니다. 또는 매개 변수로 backgroundworker를 호출 할 수 있습니다.

에 대한 자세한 내용은 MSDN과 같습니다. 매개 변수를 BackgroundWorker으로 보내는 것에 대해서는 this question입니다.

+0

고마워요. BGW에 호출하기 전에 변수를 전달하는 방법을 읽으려고합니다. – Zoltan