Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<
for (int i = 0; i < 7; i++)
                {
                    Thread thread = new Thread(() => GetRecordsInTask(rateTableId, tmpRateCardColNo, tmpcategoryId, numofrecords, dvm, TotalRows));
                    thread.Start();
                    numofrecords = numofrecords + 100000;
                }

i need to fetch 100000 per thread from DB
so i have created a thread inside for the loop,
numofrecords
tells to stored procedure how many records need to fetch, but my store procedure will fetch only 590000 records out of 690000, if i same thread run without loop for 7 times it works fine

What I have tried:

still looking at store procedure
Posted
Updated 21-Mar-23 3:05am
Comments
Chris Copeland 21-Mar-23 7:24am    
I have plenty of questions about this, as this does not seem like a good solution. However, my main concern is how are you using the numofrecords variable in your GetRecordsInTask method? I get the feeling you're spawning 7 threads, where #1 loads 0 records, #2 loads 100000 records, #3 loads 200000 etc..
Dave Kreskowiak 21-Mar-23 9:11am    
Question... Why are you fetching 700,000+ records over 7 threads? Threading is not going to make the end result show up much faster than if you did one query in a single thread.
PIEBALDconsult 21-Mar-23 10:53am    
To add to what the others have said, won't each Thread get garbage-collected pretty much as soon as you assign a new Thread to the thread variable? I don't see how any of that can be reliable at all.
When I need multiple Threads, I use an array of Threads, assign each, start each, and then Join each to know when they have all completed.
But I agree that this is not a situation where multiple Threads will help and will likely decrease performance. Is there a bottle-neck you are trying to resolve? Is it the database engine? The network? Or any processing you are doing on the data once it has been received?

Threading is not a magic bullet, it has to be planned and considered carefully - particularly when you are starting a bunch of them in a loop - because the parameters aren't actually converted to values until the thread is ready to go.

If I replace your code with this:
using System;
using System.Threading;				
public class Program
{
	public static void Main()
	{
		int numofrecords = 0;
		for (int i = 0; i < 7; i++)
		{
			Thread thread = new Thread(() => Console.WriteLine("Thread {0}, {1}",i, numofrecords));
			thread.Start();
			numofrecords= numofrecords + 100000;
			Console.WriteLine("{0} : {1}", i, numofrecords);
		}
		Thread.Sleep(2000);
	}
}
The output you expect is this:
0 : 000000
Thread 0, 100000
1 : 100000
Thread 1, 200000
2 : 200000
Thread 2, 300000
3 : 300000
Thread 3, 400000
4 : 400000
Thread 4, 500000
5 : 500000
Thread 5, 600000
6 : 600000
Thread 6, 700000
The output I get is probably not what you expected:
0 : 100000
Thread 1, 100000
Thread 1, 100000
1 : 200000
2 : 300000
Thread 3, 300000
3 : 400000
Thread 4, 400000
4 : 500000
Thread 5, 500000
5 : 600000
Thread 6, 600000
6 : 700000
Thread 7, 700000
The first thread didn't get the values you expected because there is no locking, no thread safety at all - and the execution order is non-deterministic.
Meaning that if I run it again, I get different results:
0 : 100000
Thread 1, 100000
1 : 200000
Thread 2, 200000
2 : 300000
Thread 3, 300000
3 : 400000
Thread 4, 400000
4 : 500000
Thread 5, 500000
5 : 600000
Thread 6, 600000
6 : 700000
Thread 7, 700000
Which is better, but still not what you expected because the "i == 0" thread number never appears - it has already changed by the time the value of i has been taken and passed to the thread.

Try using Parallel.For instead, and multiply i by the block size instead of using two separate counters: Parallel.For Method (System.Threading.Tasks) | Microsoft Learn[^]
 
Share this answer
 
The recommended method nowadays is using Tasks, you can chain them together as shown in this example:
Chaining tasks using continuation tasks | Microsoft Learn[^]
 
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