I have a program where I have multiple devices plugged in via USB and are connected to my program with HIDSharp. The program I have essentially creates multiple instances of "APData" class and adds the instances to a list to keep track of those APData instances. From there the GUI waits for me to press the start button and when I do the units that are connected and selected for a test will be moved over to a gridview on the GUI and each APData runs in a task to test the device for a certain parameter every 40ms or so. Essentially I have a loop that creates tasks and at the end I use WaitAll to wait for each task to complete. The thing is when I only have 2 units the program seems to work perfectly fine. When I move up to 8 the program will begin and appear to work fine but you can clearly see that some of the units begin to slow down or start off very slowly collecting data. This becomes very apparent when after a minute the program and computer begin to slow down and ultimately freeze. I even received the blue screen of death which made me really frustrated that I cannot get this completed.
I have tried isolating the GUI to see if that had anything to do with it but that did not appear to be the case. The class in which the tasks call on is also static and I have made sure that the only data being shared between the tasks is the list that keeps track of the units along with there data that is collected and associated with APData.
At this point I have no idea how I should proceed. I have tried those two things above, but like I said, the GUI and computer ultimately freeze. I suspect that I possibly need to change some parts in the test itself into tasks but that is just a guess.
Here is the code that I call which creates a task inside a foreach loop and runs a test on each device.
foreach (APData ap in lst)
{
bool inputmode = ConnectionManager.StartAutomaticSampling(ap);
var t1 = new Task(() =>
{
while (ConnectionManager.ComparedTime < 1)
{
ConnectionManager.BeginNoiseTest(ap,APNG_Application.Program.unit, inputmode);
date = DateTime.Now; // Update time.
ConnectionManager.ComparedTime = DateTime.Compare(date, endTime);
}
ap.DoneBeingTested = true;
ConnectionManager.NoiseTestEnded(ap, APNG_Application.Program.unit);
CalculateSNR(APNG_Application.Program.unit, ap);
saveFile(APNG_Application.Program.unit, ap);
});
t1.Start();
}
Task.WaitAll(Task.Delay(timeLimit));
Running this piece of code ConnectionManager.BeginNoiseTest(ap,APNG_Application.Program.unit, inputmode); begins the test for each APData inside the foreach loop and is the main culprit for freezing the program and computer.
Is my understanding of Tasks wrong? I thought creating multiple tasks can make things run a little fast and asynchronous as well? Should I be using threads maybe? Is my CPU usage being consumed by just assigning 8 tasks?
If there is any piece of code you wish to see just comment and I will be sure to reply with it!