Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
on button click my page takes alot of time to run .
so im looking forward to do multithreading but i have no idea of how to use it .
Im a fresher

What I have tried:

i have tried
{
Thread t1= new Thread (delegate()
{
//code
});
t1.Start();
}

but it doesnt worked for me
Posted
Updated 16-Nov-18 3:24am
Comments
sachin.vishwa90 16-Nov-18 7:12am    
you need to mention what you have tried?
basically you will have to create a separate thread for each method and run your thread so your code shouldn't be waiting for each other.
Richard Deeming 16-Nov-18 11:45am    
If that's your real Google API key that you've just posted to a public forum, then you should revoke it and get a new one ASAP!

Based on the code in your comments, multi-threading is not going to help you in this case. Your code is not CPU-bound; it's network bound. Adding another thread will just add extra overhead for no benefit.

Normally in this situation, you would speed things up by making multiple network requests in parallel. Unfortunately, since the second network request relies on data from the response of the first request, you can't even do that. You have to wait for the first request to complete before you can make the second request.

The speed of your code is going to be limited by the speed of the two network requests. There's not much you can do about that, except perhaps using async / await combined with RegisterAsyncTask, to ensure that you're not blocking requests from other users while you're waiting for the responses.

Page.RegisterAsyncTask(PageAsyncTask) Method (System.Web.UI) | Microsoft Docs[^]
Using Asynchronous Methods in ASP.NET 4.5 | Microsoft Docs[^]
 
Share this answer
 
Try this,

C#
Thread mythread = new Thread(new ThreadStart(<YOUR_FUNCTION_NAME>));
mythread.SetApartmentState(ApartmentState.STA);
mythread.Start();
 
Share this answer
 
Comments
Shrikant S 16-Nov-18 8:40am    
I am considering the codes written as correct one.

Your code is one of the way to do threading, if the codes inside thread has the possibility of being called by some other thread as well then creating a function and putting codes in it would be the more appropriate way.
Hi,

Do not do multithreading if you are fresh. First, read and understand what exactly you are doing. Threads are Evils so it is better to first get a good knowledge about them. I would suggest reading the below link. However, google will bring you lots of other interesting things in this field.

Threading in C# - Free E-book[^]

Cheers,
AH
 
Share this answer
 
you need to read this article once Smart Thread Pool[^]
 
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