Background worker while loop

后端 未结 4 1961
渐次进展
渐次进展 2021-01-26 18:29

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.

相关标签:
4条回答
  • 2021-01-26 18:53

    Are you looking for

    while(true)
    
    0 讨论(0)
  • 2021-01-26 18:55

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-26 19:01

    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.

    0 讨论(0)
  • 2021-01-26 19:01

    Use System.Windows.Forms.Timer

    0 讨论(0)
提交回复
热议问题