user222427
user222427

Reputation:

Background worker while loop

What i'm trying to create is a background worker that executes a few processes every 30seconds. But I want this while loop to execute for as long as the program is launched. here is what I am using:

    private void watcherprocess1()
    {
        backgroundWorker1.RunWorkerAsync();

    }  
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while(
        Thread.Sleep(30000);
        specficView2();
        makeFormlist2();
        populateListview2();
    }

My main issue is ai can think of a good while loop to keep this running

Upvotes: 0

Views: 10665

Answers (4)

SnIcK
SnIcK

Reputation: 304

Use System.Windows.Forms.Timer

Upvotes: 3

ParmesanCodice
ParmesanCodice

Reputation: 5035

I agree with Bobby that a System.Threading.Timer (or a System.Timers.Timer) is probably best suited to this job.

But if you must create a use a worker thread, I'd rather create one specially for the task. Running a BackGroundWorker (which is a ThreadPool Thread) is a while loop for the lifetime of your application doesn't sit well with me.

Maybe something like this:

private void watcherprocess1()
{        
    Thread thread = new Thread(new ThreadStart(this.Work));
    thread.IsBackground = true;
    thread.Name = "My Worker.";
    thread.Start();
}  

private void Work()
{
    while(true)
    {            
        specficView2();
        makeFormlist2();
        populateListview2();
        Thread.Sleep(30000);
    }
}

Upvotes: 3

Adriaan Stander
Adriaan Stander

Reputation: 166486

Are you looking for

while(true)

Upvotes: 5

Bobby
Bobby

Reputation: 11576

I think a Timer from the Threading-Namespace would be much better suited for this. It will tick asynchron, and you can spawn of the process from the Tick-Event.

Upvotes: 6

Related Questions