0

So I have multiple threads trying to get a response from a resource, but for some reason - even though they are running in seperate threads, each response will only return when all others are either still waiting or closed. I tried using WebResponse response = await request.GetResponseAsync(); but first of all that seems redundant to me, since I'm already running seperate threads, and also visual studio tells me

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

What's going on here?

EDIT (Code):

Start method (called from a single thread)

public void Start()
{
    if (!Started)
    {
        ByteAt = 0;
        request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "GET";
        request.AddRange(ByteStart, ByteStart + ByteLength);
        downloadThread = new Thread(DownloadThreadWorker);
        downloadThread.Start();
        Started = true;
        Paused = false;
    }
}

Download threads:

private void DownloadThreadWorker()
        {
            WebResponse response = request.GetResponse();
            if (response != null)
            {
                if (!CheckRange(response))
                    Abort(String.Format("Multi part downloads not supported (Requested length: {0}, response length: {1})", ByteLength, response.ContentLength));
                else
                { ...
pixartist
  • 1,137
  • 2
  • 18
  • 40
  • is the resource that you are calling capable of servicing only one request at a time or can it handle multiple requests at same time ? – Max Mar 17 '14 at 17:18
  • Since you didn't provide hardly any code (please always provide more then 1 line of code!!!) I have to ask, is `request` per thread? – Erik Philips Mar 17 '14 at 17:21
  • The resource can handle multiple connections. The requests are created in a single thread, the responses are created in individual threads. – pixartist Mar 17 '14 at 17:24
  • 1
    Try to set `ServicePointManager.DefaultConnectionLimit` – L.B Mar 17 '14 at 17:32
  • Beyond what [LB2 Mentioned in his answer](http://stackoverflow.com/a/22461101/209259) (which could be correct), calling Async on a Webrequest will most likely [NOT create another thread and making your own threads can hurt performance](http://stackoverflow.com/a/4382489/209259). You should take a look at [Making Asynchronous Requests with WebRequest](http://msdn.microsoft.com/en-us/library/86wf6409(v=vs.110).aspx) – Erik Philips Mar 17 '14 at 17:34

1 Answers1

0

Per HTTP 1.1 RFC a client should make no more than 2 concurrent connections. Not sure about the latest versions of IE, but previously IE honored this limitation (could be changed via a registry key) and only had no more than 2 connections to the same host at any one time. This could be the limitation you're experiencing...

Or try setting ServicePointManager.DefaultConnectionLimit above 2.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
LB2
  • 4,802
  • 19
  • 35