Multi-threaded Task stops after 150 tasks
-
I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael
public Boolean LoadData()
{
Boolean bComplete = false;
Int32 nTotalData = 10000;
this.tasks = new Task[nTotalData];
DateTime dtStartTime = DateTime.Now;ProcessingBarsCS.StartTime(); var parent = Task.Factory.StartNew(() => { dtStartTime = DateTime.Now; for (Int32 nData = 0; nData < nTotalSymbols; nData++) { bComplete = GetData\_Task(nData, dtStartTime, nTotalData); } }); parent.Wait(); ProcessingBarsCS.StopTime(); return bComplete; } Boolean GetData\_Task(Int32 nData, DateTime dtStartTime, Int32 nTotalData) { DataCS dataCS = new DataCS(); tasks\[nData\] = Task.Factory.StartNew(() => { nData = dataCS.Data(); }) .ContinueWith(antecendent => ProcessingData(nData)); // \*\*\*\*\* tasks\[nData\].Wait(); // Wait for all task to finish calculations try { Task.WaitAll(tasks); SetData\_ElapsedTime(dtStartTime, nTotalData); } catch { } return true; }
-
I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael
public Boolean LoadData()
{
Boolean bComplete = false;
Int32 nTotalData = 10000;
this.tasks = new Task[nTotalData];
DateTime dtStartTime = DateTime.Now;ProcessingBarsCS.StartTime(); var parent = Task.Factory.StartNew(() => { dtStartTime = DateTime.Now; for (Int32 nData = 0; nData < nTotalSymbols; nData++) { bComplete = GetData\_Task(nData, dtStartTime, nTotalData); } }); parent.Wait(); ProcessingBarsCS.StopTime(); return bComplete; } Boolean GetData\_Task(Int32 nData, DateTime dtStartTime, Int32 nTotalData) { DataCS dataCS = new DataCS(); tasks\[nData\] = Task.Factory.StartNew(() => { nData = dataCS.Data(); }) .ContinueWith(antecendent => ProcessingData(nData)); // \*\*\*\*\* tasks\[nData\].Wait(); // Wait for all task to finish calculations try { Task.WaitAll(tasks); SetData\_ElapsedTime(dtStartTime, nTotalData); } catch { } return true; }
My guess is that you had a
SystemOutOfMemoryException
. Try to check the Event Viewer for possible error messages that the application has thrown. Also it would be wise to set a limit for the number of tasks that will be created so as not to overload your machine.Signature construction in progress. Sorry for the inconvenience.
-
I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael
public Boolean LoadData()
{
Boolean bComplete = false;
Int32 nTotalData = 10000;
this.tasks = new Task[nTotalData];
DateTime dtStartTime = DateTime.Now;ProcessingBarsCS.StartTime(); var parent = Task.Factory.StartNew(() => { dtStartTime = DateTime.Now; for (Int32 nData = 0; nData < nTotalSymbols; nData++) { bComplete = GetData\_Task(nData, dtStartTime, nTotalData); } }); parent.Wait(); ProcessingBarsCS.StopTime(); return bComplete; } Boolean GetData\_Task(Int32 nData, DateTime dtStartTime, Int32 nTotalData) { DataCS dataCS = new DataCS(); tasks\[nData\] = Task.Factory.StartNew(() => { nData = dataCS.Data(); }) .ContinueWith(antecendent => ProcessingData(nData)); // \*\*\*\*\* tasks\[nData\].Wait(); // Wait for all task to finish calculations try { Task.WaitAll(tasks); SetData\_ElapsedTime(dtStartTime, nTotalData); } catch { } return true; }
Walking this code through:
- You start a new
Task
; - You call
GetData_Task
10000 times in a loop; GetData_Task
creates a newTask
and immediately waits for it to complete;GetData_Task
then waits for all 10000 tasks - most of which haven't been created yet - to complete, catching and ignoring theArgumentException
thrown when theWaitAll
method encounters the firstnull
element in the array;- The
LoadData
method then waits for the parentTask
to complete;
No wonder your code is slow! This code looks like a suitable candidate for the Parallel class[^]:
public bool LoadData()
{
const int nTotalData = 10000;DateTime dtStartTime = DateTime.Now; ProcessingBarsCS.StartTime(); Parallel.For(0, nTotalData, nData => { var dataCS = new DataCS(); var result = dataCS.Data(); ProcessingData(result); }); SetData\_ElapsedTime(dtStartTime, nTotalData); ProcessingBarsCS.StopTime(); return true;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
- You start a new