I want to launch a new loop task in backgroundWorker

Asked 2 years ago, Updated 2 years ago, 48 views

I would like to start the periodic processing task after initializing in the backgroundWorker and let the screen know the progress.The initialization process was successful, but the periodic processing task failed to start successfully with the error 'System.InvalidOperationException'.
The following are the main sources:
--- I was able to resolve it using the controller's Invoke, See here

Main Screen

using System;
using System.Collections.General;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BGThread
{
    public partial class Form 1:Form
    {
        public Form 1()
        {
            InitializeComponent();
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgse)
        {
            backgroundWorker1.ReportProgress(0, "startForm init--"); 
            Console.WriteLine("startForm init--");
            Thread.Sleep(5000);

            LoopWork lw = new LoopWork (backgroundWorker1);


        }

        private void backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgse)
        {
            string msg = (string)e.UserState;
            textBox1.Text=msg;
        }
    }
}

Periodic Tasks

using System;
using System.Collections.General;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BGThread
{
    public class LoopWork
    {
        private BackgroundWorker bgWorker;
        public LoopWork (BackgroundWorker bgWorker)
        {
            This.bgWorker = bgWorker;
            init();
        }

        private void init()
        {
            task1();
            task2();
        }

        private void task1()
        {
            Task.Factory.StartNew()=>
            {
                while(true)
                {
                    bgWorker.ReportProgress(0, "taks1--"+DateTime.Now.ToString("hh:mm:ss"));
                    Console.WriteLine("taks1--"+DateTime.Now.ToString("hh:mm:ss"));
                    Thread.Sleep (new TimeSpan(0,0,10));
                }
            });
        }
        private void task2()
        {
            Task.Factory.StartNew()=>
            {
                while(true)
                {
                    bgWorker.ReportProgress(0, "taks2--"+DateTime.Now.ToString("hh:mm:ss"));
                    Console.WriteLine("taks2--"+DateTime.Now.ToString("hh:mm:ss"));
                    Thread.Sleep (new TimeSpan(0,0,15);
                }
            });
        }
    }
}

c#

2022-09-30 21:19

1 Answers

The BackgroundWorker class is for long processing in the background.You can include the actions you want to take in the DoWork event.When this DoWork event completes, the BackgroundWorker instance is discarded after the RunWorkerCompleted event is processed.

In this example,

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgse)
{
    backgroundWorker1.ReportProgress(0, "startForm init--"); 
    Console.WriteLine("startForm init--");
    Thread.Sleep(5000);

    LoopWork lw = new LoopWork (backgroundWorker1);
}

The BackgroundWorker has finished calling this LoopWork constructor. You should not start a new task in Task.Factory.StartNew() but should run it in the thread.


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.