ankit Gupta
ankit Gupta

Reputation: 323

"Object reference not set to an instance of object" error in callback

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.

Upvotes: 2

Views: 2272

Answers (1)

James
James

Reputation: 82136

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.

Upvotes: 1

Related Questions