Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I applied the following code for multithreading...Is this correct?

protected void Button1_Click(object sender, EventArgs e)
   {
       try
       {
           Thread tid1 = new Thread(new ThreadStart(this.Thread1));
           Thread tid2 = new Thread(new ThreadStart(this.Thread2));

           tid1.Start();
           tid2.Start();
       }
       catch
       {
       }
   }
   public void Thread1()
   {
       try
       {
           for (int i = 0; i < 100; i++)
           {
               ListBox1.Items.Add(i.ToString());
               Thread.Sleep(10);
           }
       }
       catch (Exception ex)
       {
           Label1.Text = ex.Message;
       }
   }
   private void Thread2()
       {
           try
           {
               for (int i = 0; i < 100; i++)
               {
                   ListBox2.Items.Add(i.ToString());
                   Thread.Sleep(50);

               }
           }
           catch
           {
           }
       }

Thanks
Posted
Comments
Mehdi Gholam 30-Sep-11 1:49am    
What are you getting?
sreenathpktr 30-Sep-11 2:18am    
I am getting 0 in the list boxes..
Timberbird 30-Sep-11 3:14am    
You have already asked this question. Have you tried to wait on your threads, calling tid1.Join() and tid2.Join() at the end of Button1_Click() try block?
sreenathpktr 30-Sep-11 3:55am    
If i avoid the Thread.sleep it works correctly.Now it's correct multithreading?
Timberbird 30-Sep-11 4:03am    
No it's not. Multithreading should serve some particular purpose. In your case there are two threads running on server in a hope they can finish their job before response is sent to client :)

1 solution

Sorry, NO it is not correct.

1) Your web application doesn't wait on the two threads to finish, thus the reponse does not include the data added in the threads.
2) Never use Thread.Sleep there is always a better way, which doesn't use that ugly thing.
3) Never use leave a catch-block empty.
 
Share this answer
 
Comments
sreenathpktr 30-Sep-11 2:18am    
Multithreading cannot be used in asp.net?
Simon Bang Terkildsen 30-Sep-11 4:52am    
Ofcourse it can, but it makes no sense to me at all.
What do you hope to gain? if your answer is using as many of your cores as possible, then that is already taken care of as each request runs on it's own thread.

The only reason, I see, to do any parallel programming on the server side of a web application, is when you're making a request (like a database query) which you know might often take a long time (100+ ms) and you have other work you can do while waiting for the response.
And then again that's not a good reason, I would never do parallel programming on the server side of a web application, if I want the web application to be concurrent then I would utilize AJAX.
Simon Bang Terkildsen 30-Sep-11 5:02am    
I have a very strong opinion when it comes to parallel programming, in general poeple seem to take it as an easy task, but it ain't, it will cause bugs that are extremely difficult to track down. Even very season developers take the parallel programming litely, yet 90% of them are not even capable of limiting the risk. (the 90% is my very own personal judgement not a fact!)

I've even heard "using threads in .net is so easy, it's so cool", well yeah it is abit more easy to play around with especially after the arrival of the TPL in .NET 4.0 but all the problems of parallel programming are still present and hasn't changed in the last 30 years.
I haven't looked at the async/await which comes in C# 5.0 so I don't know if it solves some of the problems but I highly doubt that it'll have an impact on the core issues of parallel programming.
sreenathpktr 30-Sep-11 5:41am    
I have a doubt that,the main usage of the multithreading is in parallel programming right?I was just get practice in multithreading before starting my project about parallel programming.As you said,i am using database in my project,it's a ticket reservation system,on each registering by the clients,there are more than one process should happen simultaniously,that's why i am thinking about multithreading.
Is there any better way to do that without multithreading.And i have no experince in applying multithreading in web application before.Kindly tell your suggestion.
Simon Bang Terkildsen 30-Sep-11 6:13am    
I suggest you use AJAX, asynchronous calls from the client to the server, instead of playing around with threads on the server side.

Spawning threads in a web application on the server side will not speed up your site but slow it down, because of the overhead. A web application will service multiple users at a time and as such will already distribute the work among the cores available on your system.

If you want to learn parallel programming then make windows application and start out with the TPL (see Sacha Barber's series on TPL. Or if you want to do it with a web application then use AJAX.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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