BackgroundWorker used in collection items

I used the Backgroudworker to do some work to do some labor-intensive tasks.

public void ConnectDataProvider()
    {
        bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);

    }

Another method starts a background worker:

public void StartPolling()
    {
        bgw.RunWorkerAsync();
    }

Then I processed the event:

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // do it over again
        StartPolling();
    }    

void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        // do work
        WriteData();
    }

As you can see, I started working on the completion. Now it works for one desktop.

Now I want a collection, and each item must complete this task. However, with the above concept, it will continue to work in the first working run, as it will launch the working one. I think maybe a combined timer could solve the situation to give other workflows a way.

Is BackgroundWorker a good choice? Is it common to reuse BackgroundWorker like me?

1: clair: , , , BackgroundWorker. , . .

2: , , , , , .

3: ( , ), : , gps. , . . Ihad BackgroundWorker . , , . .

. ( DoWrite). BackgroundWorker , , . , .

+3
6

? 1:1 . , .

- , ConcurrentQueue, , , , , . , , , .

, , , , , , .

+4

, while() Dowork(), , Sleep().

Bgw, .

+1

, , - , StartPolling, :

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // do it over again
    StartPolling((BackgroundWorker)sender);
}

public void StartPolling(BackgroundWorker worker)
{
    worker.RunWorkerAsync();
}

, , BackgroundWorker.

+1

progresschanged? userstate .

.

cancel, .

.

+1

, . (BackgroundWorker Timer), .

, . , .

, , , , Timer:

public void ConnectDataProvider()
    {
        timer = new Timer(new TimerCallback(tCallback), null, 0, Timeout.Infinite);            
    }

private void tCallback(object state)
    {
        timer.Dispose();
        // time consuming task
        WriteData();
        timer = new Timer(new TimerCallback(tCallback), null, 5000, Timeout.Infinite);
    }

( ) . , . WriteData() HttpWebRequest, .

: ? : ?

. WebRequest , . , .

0

Source: https://habr.com/ru/post/1715400/


All Articles