2017-12-13 3 views
0

Backgroundworker을 사용하여 내 기본 UI 스레드를 열어 두지 않고 계속 시도하려고합니다. 나는 내 코드를 실행하고 있는데, 한 번 실행되는 backgroundWorker1.RunWorkerAsync(); 둘 다에 중단 점을 설정했으며, 결코 적중되지 않는 라인과 foreach 행에 중단 점을 설정했습니다.BackgroundWorker 이벤트 실행 안 함

Backgroundworker를 사용하는 적절한 방법은 무엇입니까?

public Form1() 
{ 
    InitializeComponent(); 
    backgroundWorker1.WorkerReportsProgress = true; 
    backgroundWorker1.WorkerSupportsCancellation = true; 
} 
private void btnQuery_Click(object sender, EventArgs e) 
{ 
    grid1.Rows.Clear(); 
    backgroundWorker1.RunWorkerAsync(); 
} 
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    foreach (string name in studentRoster) 
    { 
     InsertIntoDB(); 
    } 
} 
+0

확인 확인합니다. 이미 실행 중이면 다시 실행되지 않습니다. 'if (! backgroundWorker1.IsBusy) { \t backgroundWorker1.RunWorkerAsync(); } ' – Baddack

+0

사용 중이 아닙니다. 나는 당신의 구문으로 확인했다. – IcyPopTarts

+1

'backgroundWorker1 + = backgroundWorker1_DoWork'가 있는지 확인한다. Form1.designer.cs에있을 수 있습니까? – skyoxZ

답변

0

다음은 처리기가 추가 된 코드와 일부 의견입니다. 당신의 배경 작업자가 사용중인 경우

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     backgroundWorker1.WorkerReportsProgress = true; 
     backgroundWorker1.WorkerSupportsCancellation = true; 
     backgroundWorker1.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork); 
     backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged); 
     backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted); 

    } 
    private void btnQuery_Click(object sender, EventArgs e) 
    { 
     grid1.Rows.Clear(); 
     backgroundWorker1.RunWorkerAsync(); 
    } 
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     foreach (string name in studentRoster) 
     { 
      InsertIntoDB(); 

      // You can report progress by calling the following function. 
      //backgroundWorker1.ReportProgress(int percentProgress, object userState) 
      // You can set the percentProgress to any valid integer value, 
      // and userState can be any object you want. 

      // You can also check to see if this operation has been sent a request to cancel. 
      if (backgroundWorker1.CancellationPending) 
      { 
       e.Cancel = true; 
       return; 
      } 
     } 

     // You can send information back to the main thread by setting e.Result to any object you want. 
    } 
    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     // Do something with the event that is being raised. 
     // To pass a value back through to this event, use the percentProgress and userState 
     // parameters of the ReportProgress function. 
     // the userState object that you pass will be received here as e.UserState 
    } 
    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     // This event is raised by the background worker when the DoWork method is completed. 
     // You can receive information back from the worker thread by evaluating e.Result 
    } 
} 

}