1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Multithreading with numericupdown?

Discussion in 'C, C++, C#' started by Standard Toaster, Apr 24, 2013.

  1. Standard Toaster

    Standard Toaster Regular Member

    Joined:
    Aug 29, 2009
    Messages:
    335
    Likes Received:
    190
    Hi,

    I see a lot of bots with a numericupdown to control the number of concurrent threads.

    I tried to make a similar program and don't quite think it works correctly. I used the ThreadPool and set the properties SetMaxThreads and SetMinThreads to the numericupdown value. I tested and even though threads were being fired timely as expected, it didn't have much effect on the efficiency.

    Does anyone have an example code for multithreading bots?

    What would you recommend - ThreadPool or spawning threads?

    Thanks.
     
  2. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    It depends on the type of the task your application want to carry out. Threadpool is better for small tasks(but if number of tasks are more) because it reuses threads in the pool rather than creating new thread which is kinda expensive. If you have tasks that takes a long time & few in number, You're better off creating threads manually where you don't have to worry about the overhead of thread creation.


    Check out this http://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx
     
  3. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    A pooled approach will generally be better on Windows as thread and process creation time are very high compared to Unix. What you are likely running up against is a processor-bound design that is being starved of cores. If you are processor-bound, creating an more threads will do nothing to solve your latency issues, regardless of if they are pooled or not.
     
  4. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    Code:
            public void Init(int threads)
            {
                List<Thread> threadList = new List<Thread>();
                for (int i = 0; i < threads; i++)
                {
                    threadList.Add(new Thread(Worker));
                    threadList[i].Start();
                }
                for (int i = 0; i < threads; i++)
                {
                    threadList[i].Join();
                }
            }
    
    
            private void Worker()
            {
                string current = string.Empty;
                while (true)
                {
                    if (!this.WorkerQueue.TryDequeue(out current))
                    {
                        return;
                    }
                    //DO THE WORK HERE
                }
            }
            
            private ConcurrentQueue<string> WorkerQueue;
    
    I am using something like this.
    System.Collections.Concurrent namespace is in >= .NET 4
     
    • Thanks Thanks x 1
  5. MysteryGuest

    MysteryGuest Registered Member

    Joined:
    Mar 7, 2013
    Messages:
    63
    Likes Received:
    18
    Occupation:
    Freelancer
    Location:
    Virtual World!

    This is a good example! for this single threading(Manually) you could use something like this which is great for less tasks, however takes some time to set it up.
    Code:
    
                private bool                 OpenThread = true;
    
    
    private void StartThread()
            {
                var thread = new Thread(RunThread);
                thread.Start();
            }
    
    
    private void RunThread()
    {
    while (OpenThread == false)
    {
    //Do some work
    }
    Thread.sleep(1000)
    }