Background worker while loop

后端 未结 4 2010
渐次进展
渐次进展 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: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);
        }
    }
    

提交回复
热议问题