Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,

I am implementing multithreading.
I am reading data from a text file line by line and then using threads to process these lines further.
Below is my sample code:
C#
public void HandleRequest(TextReader reader)
{
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                string[] arrUserInfo = line.Split(new char[] { ',' });
                try
                {
                    Thread objThread = new Thread(new ParameterizedThreadStart(this.Add));
                    objThread.Start((object)arrUserInfo);
                }
                catch (Exception ex)
                {
                }
            }
       }

public void Add(object arrUserInfo)
{
    //Do something
}


After running application for 1000 lines, i found out that its performance is not much different if we don't use threads.
I want to improve the performance of my application.
I read on google that ThreadPool can be helpful.
I referred http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool[^] article for ThreadPool.
But i am not able to understand how can i implement ThreadPool in my application because ThreadPool uses a For loop and my application already using a while loop to read lines from text file.

Can anyone help me here.

Thanks,
Nagendra.
Posted
Updated 23-Oct-11 0:56am
v3
Comments
Sergey Alexandrovich Kryukov 23-Oct-11 23:54pm    
Maybe this is a problem of your English usage, but what you say does not sound like truth: you do not implement multithreading -- it is already implemented by OS.
Also, "because ThreadPool uses a For loop", who told you so?
--SA

1 solution

Threading may not improve the performance of your app at all: it may even make it worse in some cases. Particularly when you are creating a large number of threads to do small (almost trivial) tasks each, the overhead involved in setting up the thread can seriously outweigh the advantages your gain. Bear in mind that in order to get performance gains, you need each thread operating on it's own processor core and that there are other demands on the availability of cores from the OS itself, other applications, and so forth, and you start to see the problem. If you want to improve performance, then you need to look at what each thread in your existing code is trying to do, and examine where you bottlenecks are likely to be. It may be that your code could be improved by just reading all the lines at a time, and using two threads to each process half the records - I don't know. Or it could be that you need to look at how you are "adding" these lines.

Just generating massive numbers of threads is unlikely to give real gains.
 
Share this answer
 
Comments
nagendrathecoder 23-Oct-11 4:45am    
I compared the timmings for each operation, reading lines is not taking time. Time is taking while processing Add method.
From a log file, i observed that only one thread at a time is processing in Add method.
In Add method, i am creating users, this normally takes around 20 sec to create a user.
In log file, every user is getting created after a gap of around 15-20 sec.
I want to know is this somehow possible that say 5 threads process Add method symoltaneously and in a period of 15-20 sec these 5 users gets created.
OriginalGriff 23-Oct-11 5:01am    
So your bottle neck is in the add method - which you didn't show us - and your timings show that only one thread at a time is running. Which is pretty much what I said :laugh:
You need to look at how you add users - why is it taking 20 seconds each? What are you doing to create a user? Is the bottle neck in a database connection (if any)? If you don't tell us, we can't help much!
nagendrathecoder 23-Oct-11 5:47am    
Creating a user means to create a database entry for that user, create a AD entry for that user and finally create a Exchange Mailbox entry for that user.
It generally takes upto 20 sec to create a user which is completely acceptable time.
My aim is to try and create more users at same time frame, so that 4-5 users gets created in 20 sec time frame.
I am calling method of a webservice to add user.
I am tring to create threads to do symoltaneous entry but only 1 thread at a time is processing.
Shmuel Zang 24-Oct-11 13:23pm    
My 5.

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