Why ThreadId changes when I set the max number of threads in a Threadpool to 1

I need a way to determine what threadId a process is running on.  Please see the following code.  I have a background worker that gets invoked when you click on button1 on a form.  If I set the max number of threads to 2, then I am expecting that the label on the form will only show 2 different threadids no matter how many times I rapidly click on the button... but that isn't happening.   Instead, I may get 3 or 4 different threadids.  

If I set the max number of threads to 1 and click on the button1 to launch background workers, then I expect that I should only see one threadid over and over gain.  Instead I get several different thread ids.

I'm not sure if my threadpool is not working or if I need to use a different way to get the threadid of the thread that is executing.

Can anyone tell me how to determine what threadid the current thread is running on.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Deleteme_Test_MultiThread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private System.ComponentModel.BackgroundWorker worker;
        private string results = "";
        private int ireturn = 1;

       
        private void button1_Click(object sender, EventArgs e)
        {
            int iThreadMax = Convert.ToInt16(this.txtMaxThreads.Text);
            ThreadPool.SetMaxThreads(1, 1);

            worker = new System.ComponentModel.BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = false;
            worker.DoWork += new DoWorkEventHandler(this.DoWork);

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.WorkCompleted);
            worker.ProgressChanged += new ProgressChangedEventHandler(this.ProgressChanged);

            //fire off worker
            worker.RunWorkerAsync();
        }
       


       private void ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           this.label1.Text = results;
       }


        private void DoWork(object sender, DoWorkEventArgs e)
        {
            worker = (System.ComponentModel.BackgroundWorker)sender;
            worker.ReportProgress(0);
            int ithread = System.AppDomain.GetCurrentThreadId();

            //int ithread = System.Threading.Thread.CurrentThread.ManagedThreadId;

            Thread.Sleep(2000);
            ireturn++;
            results = ithread.ToString() + " this is a test " + ireturn.ToString();
           
        }

        private void WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.label1.Text = results;
        }
    }
}
rcearleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudheeshthegreatCommented:
Why do you need the thread id? The actual ids can be quite random, and you cant really code based on these ids. If you just want to know whether a given thread is a background thread or not, you should use the Thread.CurrentThread.IsBackground to figure that out. But, generally, we neither need to use the thread id or the IsBackground property, when working with the BackgroundWorker.
0
rcearleyAuthor Commented:
There are 2 reasons.  One is that I need to ensure that the total number of threads that are really executing are the number of threads that I set in the threadpool.  By watching the threadids of each process, I would think that I shouldn't see more threads than the max number of threads I set in the threadpool.

The other reason is that each thread will have several worksteps that will be executed within each thread.  the threadid will help me identify where a process is in a workflow.  
0
sudheeshthegreatCommented:
Have this statement
ThreadPool.SetMinThreads(1, 1);

It seems SetMaxThreads sets the number of requests to the thread pool that can be active concurrently. What about the other requests? The might be getting queued.

SetMinThreads changes the number of idle threads which the thread pool maintains and so, in your case, this would be more relevant.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

rcearleyAuthor Commented:
I don't think it is queing the items.   With the threadpool set to 1 and the sleep set to 50 seconds, if I clicked the button to fire off the process multiple times in a row,  the DoWork is fired each time.  If it was queing the processes, then DoWork wouldn't fire until after 50 seconds from the first process.

I think either the ThreadPool statement is not working or I'm not using the right property to get the threadid.

0
sudheeshthegreatCommented:
Did you try using SetMinThreads? I just tried it out here and it works, just like you want!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rcearleyAuthor Commented:
I'm still getting the same thing.  Is this the syntax that you using:  ThreadPool.SetMinThreads(1,1);
0
rcearleyAuthor Commented:
Ok, nevermind...  It's working now.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.