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

问题描述 投票:0回答:1

因此,我正在尝试构建一个供我自己使用的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;
    }
c# ssh backgroundworker streamreader invoke
1个回答
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。如果你想我也可以为你做一个小例子如何使用它

© www.soinside.com 2019 - 2024. All rights reserved.