Click here to Skip to main content
15,887,341 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am beginner in Multi-threading in C# & confuse in current task.

My task is to read datatable and display datatable in C# console application using multi threading.

I have three threads and one datatable which have 50000 Rors.
All thread must start same time.
I want :
1st thread print 1st row.
2nd thread print 2nd row.
3rd thread print 3rd row.
again
1st thread print 4st row.
2nd thread print 5nd row.
3rd thread print 6rd row. Continue till last row.

How to implement this???
Is there any other technique to do this????

What I have tried:

I have also tried locking but it didn't work for me.
Posted
Updated 25-Aug-16 3:54am

If the threads are only reading from the DataTable you don't need locking or any other sort of thread synchronization. Just pass the DataTable to each of your threads along with a value that indicates at which row a thread should start writing every third row to the console. After starting all threads join them and your task is done.

Edit: My solution ignored that you want the rows to be displayed in order. As Dave said, with that requirement the task doesn't lend itself to multi-threading because then then threads would have to be synchronized and there would be no benefit over using a single thread. So if there should be some sort of benefit through multi-threading then this requirement would have to be dropped. (But as the display-speed of the console isn't that high it is questionable if there is any benefit through multi-threading even if the threads aren't synchronized.)
 
Share this answer
 
v3
This problem cannot be solved by threading. Without using locking, you cannot guarantee the order in which threads run. You CAN get a thread order like 1, 2, 3, 1, 3, 2, 3, 1, 2, 1, ...

Introducing locking to get the correct order just makes these threads execute synchronously, no different than a single-threaded operation.

The problem does not lend itself to being solved by threading.


Now, if you're trying to show a user 50,000 records, you shouldn't be doing that. Either filter the records down to something manageable or implement some kind of paging mechanism where you show a small block of the larger set of records.

I can't tell you what to do or how to do it because you haven't said anything about the real problem you're trying to solve.
 
Share this answer
 
v2
Comments
Sascha Lefèvre 1-Apr-16 11:06am    
5ed
If the rows reed to be printed in order then you'll probably need to use wait states. Basically it is something that causes a thread to pause until something triggers the state instructing the thread to continue. So thread 1 will only read rows 1, 4, 7 etc. When thread 1 starts it hits a wait state that causes it to wait, it's logic will be like

1 start
2 wait
3 read next row (1, 4, 7 etc)
4 if more rows are left for me reset my wait state and loop back to 2

Thread 2 will be almost identical

1 start
2 wait
3 read next row (2, 5, 8 etc)
4 if more rows are left for me reset my wait state and loop back to 2

Thread 3 you can probably guess by now.

The trick to this is triggering the wait states the starting process will trigger 1's, thread one will have to trigger two's, two will trigger three's and three's will trigger ones. That way they'll all go in sequence.

If you *don't* have to print them in sequence then that's easy, it's the code above without all the wait state stuff. Google "c# manualresentevent" to get an idea how you code these things.
resent event
 
Share this answer
 
The solution as presented is actually not feasible because the System.DataTable.Read() is not thread safe. What this means is that any attempt to read from the same reader may produce redundant, out of order results.

DataTable thread safety

If the DataTable was thread safe, to achieve proper ordering (as mentioned already) you would still have to use synchronization techniques to surround both the read and the write. That is the only way to ensure that subsequent writes were from the appropriate read.

The real world problem to this example is reading in data from a large source, then outputting it. In the case the read operation is expensive the optimal threaded solution is to use asynchronous reads to store in a temporary concurrent collection (i.e. ConcurrentBag<t>). After all data retrieval is complete, you sort the list then output synchronously. That method requires the data contain the information required to sort. If a sorted order wasn't required you could use the producer-consumer pattern to improve output performance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900