2017-12-19 4 views
0

그래서 내 프로젝트에 필요한 작업을 단순화 한 SSH 회사를 구축하려고합니다. (전자 상거래 웹 사이트에서 작업 중이며 cron 오류로 인해 수동으로 다시 색인해야합니다) SSH 도구는 일을 더 쉽게 만들어야합니다.C# BackgroundWorker streamreader가 몇 초마다 반복합니다.

Click here to see a screenshot of the program

는 내가 명령을 실행할 때 내 프로그램이 나에게 라이브 결과를주고 싶다. 저는 현재 타이머가 해결책이 될 수 있다고 생각합니다. 슬프게도 나는 성공하지 못했습니다. 뿐만 아니라 여러 가지 시도를했지만 어떤 이유로 "ProgressChanged"는 아무 것도하지 않는 것처럼 보입니다.

약간의 스레드 This one for example, 을 읽었습니다.하지만 어떻게 할 수 있습니까?하지만 나 자신을 위해 할 수는 없으므로 누군가 내게이 문제를 해결하도록 도울 수 있습니다.

private void btnUitvoeren_Click(object sender, EventArgs e) 
    { 
     backgroundWorker.RunWorkerAsync(); 

     lblStatus.Text = "Process is bezig... een moment geduld aub"; 
     lblStatus.ForeColor = Color.Orange; 
    } 

    public void ReindexCommand() 
    { 
     var cmd = client.CreateCommand(txtBoxInput.Text); 
     var result = cmd.Execute(); 
     this.Invoke(new Action(() => 
     { 
      rTxtBoxOutput.Text += result; 

      var reader = new StreamReader(cmd.ExtendedOutputStream); 
      rTxtBoxOutput.Text += "\n" + reader.ReadToEnd(); 
     } 
     )); 
    } 

    public void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) 
    { 
     ReindexCommand(); 
    } 

    private void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) 
    { 
        // Gonna work on this   
    } 

    private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) 
    { 

     lblStatus.Text = "Process Compleet"; 
     lblStatus.ForeColor = Color.Green; 
    } 
+2

'backgroundWorker_ProgressChanged'는 마술에 의해 호출되지 않습니다. 'backgroundWorker.ReportsProgress = true;를 설정 한 다음'backgroundWorker_DoWork' 또는'ReindexCommand'에서 진행 상황을보고 할 때마다'backgroundWorker.ReportProgress (...) '를 호출해야합니다. 이 마지막 메서드는 예를 들어 백분율 값과 progresschanged 처리기에 전달할 몇 가지 추가 정보를 취합니다. –

+0

나는 그것을 시도했다, 슬프게도 작동하지 않았다. 내가 background.ReportProgress (0, result);를 추가했다 .Invoke와 내 ProgressChanged에'rTxtBoxOutput.Text = (e.UserState.ToString()); '를 추가했으나 작동하지 않는다. 왜 정확히 모르겠다. ProgressChanged가 내 richTextBox를 업데이트 할 수 있습니까? 내가 어떻게이 일을 성취 할 수 있는지 아십니까? @ RenéVogt –

+0

예. 가능해야합니다. 'backgroundWorker.ReportsProgress = true; '를 설정 했습니까? –

답변

0

배경 작업자를 올바르게 사용하는 방법을 설명해 드렸습니다. 배경 자료를 처리하는 데 필요한 모든 객체는 RunWorkerAsync -Method에 대한 인수로 전달되어야합니다. 당신은 DoWork-방법에 UI 제어에 액세스 할 수 없습니다 :

private void InitializeBackgroundWorker() 
    { 
     backgroundWorker = new BackgroundWorker 
     { 
      WorkerReportsProgress = true 
     }; 
     backgroundWorker.DoWork += backgroundWorker_DoWork; 
     backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged; 
     backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; 
    } 
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    // get your result-object and cast it to the desired type 
    string myStringResult = (string)e.Result; 

    // and here we are back in the UI-Thread 
} 

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    // Now we are in the UI-Thread 
    // get the passed progress-object and cast it to your desired type 
    string myStringObject = (string)e.UserState; 

    // do some UI-Stuff... 
} 

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    // get your argument 
    string input = (string)e.Argument; 

    // do your async stuff 
    // to call the progress-changed handler call 
    ((BackgroundWorker)sender).ReportProgress(0, null /*Object to pass to the progress-changed method*/); 


    // to pass an object to the completed-handler call 
    e.Result = null; // null = your object 

} 

private void btnUitvoeren_Click(object sender, EventArgs e) 
{ 
    backgroundWorker.RunWorkerAsync(txtBoxInput.Text); // pass the input-string to the do-work method 

    lblStatus.Text = "Process is bezig... een moment geduld aub"; 
} 

당신이 비동기 await를 살펴 것보다 .NET 4.5 이상을 사용하는 경우. 원한다면 이걸 사용하는 작은 예제를 만들 수도 있습니다