2

I am using IHttpAsyncHandler and XMLHTTPRequest to push messages to client with the help of following URL: http://www.codeproject.com/Articles/42734/Using-IHttpAsyncHandler-and-XMLHttpRequest-to-push but I make some changes, actually this example based to one client only and I have to send messages to multiple clients so I make these changes

public void ProcessRequest(HttpContext context)
{
        var recipient = context.Request["recipient"]; 
        lock (_obj)
        {               
            string[] totalfrnds = ("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").Split(',');//<- This is just an example for many clients

            foreach (var a in totalfrnds)
            {
                var handle = MyAsyncHandler.Queue.Find(q => q.SessionId == a);//<- check who's client is online or not

                if (handle != null)
                {
                    handle.Message = context.Request["message"];

                    handle.SetCompleted(true);
                }
            }
        }
 }

Error Snapshot

Now I have two problems

  1. How to resolve this type of error? This is not a permanent error, it occurs randomly.

  2. What is the limit of asynchronous thread in W3wp.exe, as when it exceeds 25 request it can't open next request and I have to reset the IIS to start again the application.

ankit Gupta
  • 313
  • 1
  • 5
  • 19

1 Answers1

1

How to resolve this type of error?

You have a race condition - change your SetCompleted method to take a copy of Callback at the time of invocation e.g.

var handler = Callback;
if (isCompleted && handler != null)
{
    handler(this);
}

What is the limit of asynchronous threads in W3wp.exe

This is configured in the machine.config file on your server, specifically the maxWorkerThreads element in the <processModel> section, according to the docs the default is 20:

Configures the maximum amount of worker threads to be used for the process on a per-CPU basis. For example, if this value is 25 on a single-processor server, ASP.NET uses the runtime APIs to set the process limit to 25. On a two-processor server, the limit is set to 50. The default is 20. The value of maxWorkerThreads must be equal to or greater than the minFreeThread attribute setting in the configuration section.

Community
  • 1
  • 1
James
  • 80,725
  • 18
  • 167
  • 237
  • thanks for the answer. can u explain me briefly with proper example. – ankit Gupta Feb 20 '13 at 05:28
  • 2
    @ankitGupta "*give me a proper solution*" - not quite the correct way of asking for help! Besides I *have* gave you a proper solution. – James Feb 20 '13 at 08:54
  • actually i am trying to solve this error from last few days, but nothing find solution that's why i am asking u for the solution. i didn't understand the solution. can u plz describe it – ankit Gupta Feb 20 '13 at 09:12
  • @ankitGupta you asked 2 questions - which to the best of my knowledge I have answered - I don't know what else you want me to tell you as your question hasn't changed. – James Feb 20 '13 at 23:42
  • Your Solution is not working. Again the same error be appeared. – ankit Gupta Feb 21 '13 at 04:37
  • @ankitGupta So your still getting the NRE with the code I suggested? Could you elaborate a little more? – James Feb 21 '13 at 09:14