Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a web application with ASP .Net WEB API for integrating GMail API.
I am reading threads then reading messages on each thread id with a loop of threads.
It is taking too much loading to get response. Please help me if i did anything wrong.

And i want to know the beast process to read and display messages (gmail mails) in Front end (browser).

What I have tried:

ListRequest req = gs.Users.Threads.List(me);
req.LabelIds = "INBOX";
req.MaxResults = 50;
res threadResponse = req.Execute();
foreach (Thread eachThread in res.Threads)
{
GetRequest req = gs.Users.Threads.Get(EmailAddress, eachThread.Id);
req.Format = ThreadsResource.GetRequest.FormatEnum.Metadata;
List<string> metaData = new List<string>() { "From", "Date", "Subject" };
req.MetadataHeaders = metaData;
req.Fields = "id, messages(id,labelIds/*,payload/headers(name,value))";
Thread thcon = req.Execute();
Message lmsg = thcon.Messages.LastOrDefault();
EmailList.Add(lmsg);
}
Posted
Updated 21-Oct-22 1:43am

1 solution

Threading is not a Magic Bullet that will unconditionally increase your app speed: A thread needs a free core in order to run, so if you spin up X threads and there are X / 2 cores in the machine, you cannot run all thread simultaneously, and at least half of them will be blocked waiting for a free core. How many exactly depends on the load on the rest of the system: all the other threads are also competing for free cores. And since threading adds overhead (both memory and processing) generating too many threads can actually make your code run slower than it did as a single threaded task - particularly if you use any external processing, which is implied by your code.

I'd start by running it as a single thread task, and using the Stopwatch class to find out exactly where your bottleneck is: then concentrate on that area to find performance improvements. Just bouncing straight to multithreading won't necessarily improve your performance at all, depending on what your Request.Execute is actually doing, and what resources it requires.
 
Share this answer
 
Comments
Richard Deeming 21-Oct-22 8:11am    
I'm not convinced the question is about threads; I suspect it's about threads - email conversations. :)

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