c#BackgroundWorker streamreader每隔几秒重复一次

时间:2017-12-19 13:18:48

标签: c# ssh backgroundworker streamreader invoke

所以我正在尝试建立一个供自己使用的SSH公司,这简化了我项目的工作(我正在开发一个电子商务网站,由于cron错误我必须手动重新索引)所以这个SSH工具应该可以制作东西更容易。

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;
    }

1 个答案:

答案 0 :(得分:0)

我为您提供了一个如何正确使用后台工作程序的小例子。注意所有执行background-stuff所需的objets应该作为参数传递给RunWorkerAsync - Method。您无法在DoWork-Method中访问UI-Control:

 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";
}

如果使用.net 4.5或更高版本,则应查看async-await。如果你想我也可以为你做一个小例子如何使用这个