I hit a stumbling block in my foray into multithreading. I think I know what the problem is, but I canβt determine how to solve it. But I could be wrong.
As a result, I have flows of producers and consumers. Manufacturer threads collect data from an external source in datatables, and then put them in a collection. The consumer then captures data from the collection. I use BlockingCollection as a public static collection, so that it can be accessed by both threads, which exists in two different classes. I will show the main parts of the code, and then explain what is and does not work.
Release Topic:
try
{
dataTable.Clear();
adapter.Fill(dataTable);
dataCaptured = true;
timeout = 0;
ThreadInfo.setCurrentDate(startDate);
ThreadInfo.dataTableCollection.Add(dataTable);
}
Consumer flows
while(true)
{
DataTable testTable = ThreadInfo.dataTableCollection.Take();
foreach (DataRow datarow in testTable.Rows)
{
foreach (var item in datarow.ItemArray)
{
Console.WriteLine(item);
}
}
}
, , , , . , count , add. , , - , . , take , . , "" datatable.
, foreach, . , :
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Collection was modified; enumeration operation might not execute.
Source=System.Data
StackTrace:
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at pullPlexTable.InputThreads.dataConsumerThread() in \\srv-file01\users$\dkb\Visual Studio 2013\Projects\pullPlexTable\pullPlexTable\InputThread.cs:line 39
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
, , datatable. , , , .movenext() , , ?
, . , . , datatable datatable, take , , . , datatable , , , datatable , . , , . - ?
, .