[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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;
        }
    }
}
0
rcearley
Asked:
rcearley
  • 4
  • 3
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now