简体   繁体   English

C#BackgroundWorker streamreader每隔几秒钟重复一次

[英]c# BackgroundWorker streamreader repeat every few seconds

So I am trying to build an SSH company for my own use that simplifies things for my project ( I am working on an ecommerce website and I have to reindex manually due to cron errors ) so this SSH tool is supposed to make things easier. 因此,我正在尝试建立一个自己使用的SSH公司,以简化项目工作(我在电子商务网站上工作,由于cron错误,我必须手动重新编制索引),因此该SSH工具应该可以使事情变得更容易。

点击此处查看该程序的屏幕截图

I want my program to give me a live result when I execute a command. 我希望我的程序在执行命令时能给我带来实时的结果。 I currently think that a timer could be the solution, sadly I haven't been successful. 我目前认为计时器可能是解决方案,可惜我没有成功。 not only that but I've tried several things but for some reason the "ProgressChanged"seems to do nothing, atleast not when I'm trying something. 不仅如此,我还尝试了几件事,但由于某种原因,“ ProgressChanged”似乎什么也不做,至少在我尝试某事时没有。

I've read a few threads This one for example , which explains what and how, but I am not able to do it for myself, so my question was if someone could help me achieve this. 我已经阅读了一些线程,例如 ,该线程解释了什么以及如何进行操作,但是我无法自己完成,所以我的问题是是否有人可以帮助我实现这一目标。

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

I've made a small example for you how to use the background-worker correctly. 我为您提供了一个小示例,说明如何正确使用背景工。 Remark that all objets that are needed to do the background-stuff should be passed as an argument to the RunWorkerAsync -Method. 请注意,应该将执行后台工作所需的所有对象作为参数传递给RunWorkerAsync -Method。 You can not access your UI-Control in the DoWork-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";
}

If you use .net 4.5 or higher than you should take a look at async-await. 如果使用.net 4.5或更高版本,则应查看async-await。 If you want I can also make a small example for you how to use this 如果您愿意,我也可以为您举一个小例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM