# Async function with slow web reference(web service) slowing down webpage

Is there a way to make a threading/async post with a web reference not slow down the whole website just because the web reference takes a long time to return a response?

I have a send email function that gets posted async with a threading system that basically looks like this. This whole .cs file is called Mail.cs (that's where the Mail. name comes from).

public static bool SendMail(var alotOfVariablesHere)
{
MailParameters param = new MailParameters(alotOfVariablesHere);
return true;

}

static Object locker = new object();
public static void QueueSendMailAsyc(object Objparam)
{
lock (locker)
{
SendMailAsyc(Objparam);
}
}


Inside SendMailAsyc() we run our web reference:

WebReference.Service1 Converter = new WebReference.Service1();
Converter.Timeout = 3 * 60 * 1000;



The only thing the WebReference does is receiving a byte array and a file name and then returns a new byte array.

Pretty simple and it all works fine.

But recently the server were the web reference is sitting on (a totally different server from the webpage) has started getting really slow. This means that the web reference takes a long time to respond (it does respond and it does return everything like the way we want it. just that it's now a lot slower)

And this has started causing a lot of issues, like: The webpage also get really slowed down, it takes ages to navigate through the pages and so on.

Plus the fact that this one single web reference threading/async post takes so long time means that there is plenty of time to put up even more threading/async posts meaning it'll put an even bigger pressure on the server, causing it to slow down a lot.

This issue is primary that the web reference has gotten really slow lately, and that our webpage server cannot handle the pressure of too many threads/async posts.

I can't do much about the server the web reference is sitting on has started getting really slow recently.

But is there anything I can do code-wise to make my system handle this really slow web reference better?

• "After this we run SendMailAsyc function. This function itself is kinda too big to be posted here." Red flag for me. This screams "my function is very big and ugly and I know it must be split up but I can't let you because it's not relevant". What about still posting that bit of code? Jul 24 '14 at 9:17
• @Pimgd It's just a lot of strings here and there plus variable checks. And it all gets completed within 0.1 seconds. The issue here is the web reference and how to deal with it Jul 24 '14 at 9:24
• 0.1 seconds is a very, very long time for a computer. Jul 24 '14 at 11:56
• @rolfl notice the "within" part. At worst it might go up to 0.1 seconds to run. The web reference however can take up to half a minute just to respond. Jul 24 '14 at 12:01
• @HenrikP honestly I didn't read much of the question. Is the code running on the server or the client? Please explain why you made this choice.
– user12202
Jul 24 '14 at 13:08

Manually spawning threads is something you should avoid doing, especially if there's a chance that thread is just going to wait on a lock. As the remote service gets slower and your app gets busier, you'll have more and more idle threads taking up resources. You can make a single mail thread that has new params passed to it (in a thread-safe way).