-1

Everyone, As we know, "ThreadPool" is a static type, so we can't "new" a "ThreadPool. If I use the code

BackgroundWorker worker = new BackgroundWorker();

how am I supposed to add this "workers" to "ThreadPool"? Thanks

P.Roger
  • 79
  • 6
  • does [this](https://stackoverflow.com/questions/26408503/how-do-i-create-a-pool-of-background-workers) answer your question? – styx Jan 16 '20 at 06:40

1 Answers1

2

You don't. Check the following example:

        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        private void startAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }
        }

See the call to RunWorkerAsync. This handles the asynchronous execution of the background process, without you worrying about the thread pool.

More hands on information about how WPF and .net chose threads behind the scenes, can be found here: How does BackgroundWorker decide on which thread to run the RunWorkerCompleted handler?

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61