C# 连续运行 BackgroundWorker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19769584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:50:20  来源:igfitidea点击:

Running a BackgroundWorker continuously

c#multithreadingwinformsbackgroundworkerinfinite-loop

提问by user2952817

I need to be able to continuously run my BackgroundWorker. The DoWorkevent contains a pool threaded process and the OnCompleteupdates my UI.

我需要能够连续运行我的BackgroundWorker. 该DoWork事件包含一个池线程进程并OnComplete更新我的 UI。

I have not been able to find a way to infinitely loop the BackgroundWorker.RunWorkerAsync()method without the whole program freezing. Any help would be greatly appreciated.

我一直无法找到一种方法来无限循环该BackgroundWorker.RunWorkerAsync()方法而不冻结整个程序。任何帮助将不胜感激。

采纳答案by Tomtom

You have to make a loop in your DoWork-Method. To update your UI you shoud use the ProgressChanged-Method. Here is a small example how this can look like

您必须在 DoWork-Method 中进行循环。要更新您的 UI,您应该使用 ProgressChanged-Method。这是一个小例子,它看起来像

 public Test()
    {
        this.InitializeComponent();
        BackgroundWorker backgroundWorker = new BackgroundWorker
            {
                 WorkerReportsProgress = true,
                WorkerSupportsCancellation = true
            };
        backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
        backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
    }

    private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        object userObject = e.UserState;
        int percentage = e.ProgressPercentage;
    }

    private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker) sender;
        while (!worker.CancellationPending)
        {
            //Do your stuff here
            worker.ReportProgress(0, "AN OBJECT TO PASS TO THE UI-THREAD");
        }        
    }

回答by Joe

If your program is freezing, it may be because your infinitely looping background worker thread is spinning, using 100% CPU. You haven't said why you need it to run in an infinite loop, but you could start by putting a Thread.Sleepin that loop.

如果您的程序冻结,可能是因为您的无限循环后台工作线程正在旋转,使用 100% CPU。您还没有说明为什么需要它在无限循环中运行,但您可以从Thread.Sleep在该循环中放入 a 开始。

回答by Niels Schmidt

I have done this in the past when needing something to run in the background. If you try to run the backgroundworker while it is running, you will get an excpetion! That is why i make the BackGroundWorker start itself when it is done in the completed event.

过去,当需要在后台运行某些内容时,我曾这样做过。如果您尝试在后台工作器运行时运行它,您将得到一个异常!这就是为什么我让 BackGroundWorker 在完成事件中完成时自行启动。

And then it will loop forever.

然后它将永远循环。

private void Main_Load(object sender, EventArgs e)
{
   // Start Background Worker on load
   bgWorker.RunWorkerAsync();
}

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
   Thread.Sleep(1000);   // If you need to make a pause between runs
   // Do work here
}

private void bgCheck_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Update UI

// Run again
bgWorker.RunWorkerAsync();   // This will make the BgWorker run again, and never runs before it is completed.
}

回答by Alen Smith

 timer.interval=60000 // 1 min

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        timer1.Start();

    }
 private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
           //Do something
        }
        catch
        {


        }
    }