MultiThreading help
-
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
-
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
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 -
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
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!
-
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 KreskowiakThe 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,
-
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,
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 -
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 KreskowiakThank you Dave, that helps me on where to look. Much appreciated.