Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MultiThreading help

MultiThreading help

Scheduled Pinned Locked Moved C#
helpdatabase
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    solutionsville
    wrote on last edited by
    #1

    Hi, I inherited a program from a programmer that has since left the company. He built it with threading and the program crashes I think due to starting more threads than the system can handle. I am thinking that I need to implement a Thread Pool for this code, and am unsure how I should do that not having done it before, and complicating the matter of modifying the existing code. So, here is the existing code that uses the threading;

    using my.data;
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Threading;

    namespace CnaFirmware
    {

    public class DbUpdateFromField
    {
        Dictionary cnas;
        ConcurrentQueue ConsoleQueue;
        EventWaitHandle UpdateComplete;
        ClsUdpDispatcher UdpDispatcher;
        List updates;
    
        // this class starts a thread that downloads config data from all cnas, then updates the cna\_firmware table.
        class CNA
        {
            public string ip;
            public string firmware;
            public int cktid;
            public bool updated;
            public bool error;
    
            public CNA(string i, string f, int c)
            {
                this.ip = i;
                this.firmware = f;
                this.cktid = c;
                this.updated = false;
                this.error = false;
            }
        }
    
        public DbUpdateFromField(ClsUdpDispatcher disp, ConcurrentQueue q, EventWaitHandle op\_done)
        {
            UdpDispatcher = disp;
            ConsoleQueue = q;
            UpdateComplete = op\_done;
            Log("start");
            updates = new List();
            cnas = new Dictionary();
            new Thread(() => Start()).Start();
        }
    
        void Start()
        {
            GetIpList();
            GetCnaData();
        }
        void Log(string s)
        {
            ConsoleQueue.Enqueue("DbUpdate: " + s);
        }
    
        void GetCnaData()
        {
            SqlConnection DB;
            foreach (var v in cnas)
            {
                // get data from each cna, in sequence (not concurrent)
                new Thread(() => GetCnaInfo(v.Value)).Start();
            }
            // loop through all the cnas until either error or complete flag is set
            bool done;
            do
            {
                Thread.Sleep(50);
                done = tr
    
    D OriginalGriffO 2 Replies Last reply
    0
    • S solutionsville

      Hi, I inherited a program from a programmer that has since left the company. He built it with threading and the program crashes I think due to starting more threads than the system can handle. I am thinking that I need to implement a Thread Pool for this code, and am unsure how I should do that not having done it before, and complicating the matter of modifying the existing code. So, here is the existing code that uses the threading;

      using my.data;
      using System;
      using System.Collections.Concurrent;
      using System.Collections.Generic;
      using System.Data.SqlClient;
      using System.Threading;

      namespace CnaFirmware
      {

      public class DbUpdateFromField
      {
          Dictionary cnas;
          ConcurrentQueue ConsoleQueue;
          EventWaitHandle UpdateComplete;
          ClsUdpDispatcher UdpDispatcher;
          List updates;
      
          // this class starts a thread that downloads config data from all cnas, then updates the cna\_firmware table.
          class CNA
          {
              public string ip;
              public string firmware;
              public int cktid;
              public bool updated;
              public bool error;
      
              public CNA(string i, string f, int c)
              {
                  this.ip = i;
                  this.firmware = f;
                  this.cktid = c;
                  this.updated = false;
                  this.error = false;
              }
          }
      
          public DbUpdateFromField(ClsUdpDispatcher disp, ConcurrentQueue q, EventWaitHandle op\_done)
          {
              UdpDispatcher = disp;
              ConsoleQueue = q;
              UpdateComplete = op\_done;
              Log("start");
              updates = new List();
              cnas = new Dictionary();
              new Thread(() => Start()).Start();
          }
      
          void Start()
          {
              GetIpList();
              GetCnaData();
          }
          void Log(string s)
          {
              ConsoleQueue.Enqueue("DbUpdate: " + s);
          }
      
          void GetCnaData()
          {
              SqlConnection DB;
              foreach (var v in cnas)
              {
                  // get data from each cna, in sequence (not concurrent)
                  new Thread(() => GetCnaInfo(v.Value)).Start();
              }
              // loop through all the cnas until either error or complete flag is set
              bool done;
              do
              {
                  Thread.Sleep(50);
                  done = tr
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Quote:

      He built it with threading and the program crashes

      Stop right there and describe the crash, any exceptions you get, and any related messages you may dig out of the Application Event Log. Beyond this one statement, everything else you said is mere speculation without any understanding of the underlying problem. That's a recipe for a lot of wasted time. Research the problem, not the proposed "solution".

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      S 1 Reply Last reply
      0
      • S solutionsville

        Hi, I inherited a program from a programmer that has since left the company. He built it with threading and the program crashes I think due to starting more threads than the system can handle. I am thinking that I need to implement a Thread Pool for this code, and am unsure how I should do that not having done it before, and complicating the matter of modifying the existing code. So, here is the existing code that uses the threading;

        using my.data;
        using System;
        using System.Collections.Concurrent;
        using System.Collections.Generic;
        using System.Data.SqlClient;
        using System.Threading;

        namespace CnaFirmware
        {

        public class DbUpdateFromField
        {
            Dictionary cnas;
            ConcurrentQueue ConsoleQueue;
            EventWaitHandle UpdateComplete;
            ClsUdpDispatcher UdpDispatcher;
            List updates;
        
            // this class starts a thread that downloads config data from all cnas, then updates the cna\_firmware table.
            class CNA
            {
                public string ip;
                public string firmware;
                public int cktid;
                public bool updated;
                public bool error;
        
                public CNA(string i, string f, int c)
                {
                    this.ip = i;
                    this.firmware = f;
                    this.cktid = c;
                    this.updated = false;
                    this.error = false;
                }
            }
        
            public DbUpdateFromField(ClsUdpDispatcher disp, ConcurrentQueue q, EventWaitHandle op\_done)
            {
                UdpDispatcher = disp;
                ConsoleQueue = q;
                UpdateComplete = op\_done;
                Log("start");
                updates = new List();
                cnas = new Dictionary();
                new Thread(() => Start()).Start();
            }
        
            void Start()
            {
                GetIpList();
                GetCnaData();
            }
            void Log(string s)
            {
                ConsoleQueue.Enqueue("DbUpdate: " + s);
            }
        
            void GetCnaData()
            {
                SqlConnection DB;
                foreach (var v in cnas)
                {
                    // get data from each cna, in sequence (not concurrent)
                    new Thread(() => GetCnaInfo(v.Value)).Start();
                }
                // loop through all the cnas until either error or complete flag is set
                bool done;
                do
                {
                    Thread.Sleep(50);
                    done = tr
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        To add to what Dave said, while there are limits to how many threads can be created and that varies according to your OS, 32 /64 bit selection, and the required thread stack size, but for a 64 bit system, it's possible to generate in excess of 50,000 threads from a single process. There is an interesting explanation here: Pushing the Limits of Windows: Processes and Threads – Mark's Blog[^] But the more important question would be "why the elephant would you even try to generate that many threads, give that you probably only have 4 or 8 cores to run them on?". And in your case, why do you think it's hitting a thread limit? What have you done that leads you to that conclusion? Why has it "suddenly" started happening? Or did it ever work? Is his code finished and tested? I'd start with those questions before I even thought about changing the code in any way...

        Sent from my Amstrad PC 1640 Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Quote:

          He built it with threading and the program crashes

          Stop right there and describe the crash, any exceptions you get, and any related messages you may dig out of the Application Event Log. Beyond this one statement, everything else you said is mere speculation without any understanding of the underlying problem. That's a recipe for a lot of wasted time. Research the problem, not the proposed "solution".

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          S Offline
          S Offline
          solutionsville
          wrote on last edited by
          #4

          The crash that occurs occasionally. Is Exception Unhandled System.IndexOutOfRangeException: 'Index was outside of the bounds of the array.'. This occursis an at the GetCnaInfo(CNA cna) of the code I previously posted, where it reads updates.Add(qb.Query);. I have stepped thru the code, set break points where the query starts, and I am not finding what the issues is that is causing the IndexOutOfRangeException. Sometimes this will run all the way thru the routine and not error. Clear the field in the database and run the routine again and then I get this error. Thanks,

          D 1 Reply Last reply
          0
          • S solutionsville

            The crash that occurs occasionally. Is Exception Unhandled System.IndexOutOfRangeException: 'Index was outside of the bounds of the array.'. This occursis an at the GetCnaInfo(CNA cna) of the code I previously posted, where it reads updates.Add(qb.Query);. I have stepped thru the code, set break points where the query starts, and I am not finding what the issues is that is causing the IndexOutOfRangeException. Sometimes this will run all the way thru the routine and not error. Clear the field in the database and run the routine again and then I get this error. Thanks,

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Well, this is definitely not a thread pool or "number of threads" issue. This is more of a data management and control problem. That means, it seems like one thread is removing items from an array and another thread is asking for an item at an index that doesn't exist anymore. The calls to Thread.Sleep lead me to believe that the code is poorly written in the first place. The Sleeps were put in there as a naive way of getting around a problem that the original coder didn't understand. Thread.Sleep should never be used in the code you've posted. You said the problem shows up at updates.Add(qb.Query). I have no idea what that QueryBuilder class is and that seems like what would be throwing the exception. The reference to updates.Add is just a List and that will not throw the exception. The array bounds are managed internally by the List class and never fail like your exception describes.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            S 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Well, this is definitely not a thread pool or "number of threads" issue. This is more of a data management and control problem. That means, it seems like one thread is removing items from an array and another thread is asking for an item at an index that doesn't exist anymore. The calls to Thread.Sleep lead me to believe that the code is poorly written in the first place. The Sleeps were put in there as a naive way of getting around a problem that the original coder didn't understand. Thread.Sleep should never be used in the code you've posted. You said the problem shows up at updates.Add(qb.Query). I have no idea what that QueryBuilder class is and that seems like what would be throwing the exception. The reference to updates.Add is just a List and that will not throw the exception. The array bounds are managed internally by the List class and never fail like your exception describes.

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              S Offline
              S Offline
              solutionsville
              wrote on last edited by
              #6

              Thank you Dave, that helps me on where to look. Much appreciated.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups