【问题标题】:c# BackgroundWorker streamreader repeat every few secondsc# BackgroundWorker streamreader 每隔几秒重复一次
【发布时间】:2017-12-19 13:18:47
【问题描述】:

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

我希望我的程序在我执行命令时给我一个实时结果。我目前认为计时器可能是解决方案,遗憾的是我没有成功。不仅如此,我还尝试了几件事,但由于某种原因,“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;
    }

【问题讨论】:

  • backgroundWorker_ProgressChanged 不会被魔法调用。您需要设置backgroundWorker.ReportsProgress = true;,然后从backgroundWorker_DoWork 或ReindexCommand 调用backgroundWorker.ReportProgress(...),只要您想报告一些进度。最后一种方法采用例如百分比值和一些您想要传递给 progresschanged 处理程序的额外信息。
  • 我试过了,可惜没用。我在 this.Invoke 上方添加了 backgroundWorker.ReportProgress(0, result); 并在我的 ProgressChanged 中添加了 rTxtBoxOutput.Text = ( e.UserState.ToString()); 但是,这不起作用,我不确定为什么。 ProgressChanged 能够更新我的richTextBox 吗?你知道我怎么能做到这一点吗? @RenéVogt
  • 是的,应该可以。你设置backgroundWorker.ReportsProgress = true;了吗?

标签: c# ssh backgroundworker streamreader invoke


【解决方案1】:

我已经为你做了一个如何正确使用后台工作者的小例子。请注意,执行背景工作所需的所有对象都应作为参数传递给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。如果你愿意,我也可以为你做一个小例子来告诉你如何使用它

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2021-04-17
    相关资源
    最近更新 更多