Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint / SharePoint2010

You cannot invalidate the SPRequest object while it’s in use in SharePoint 2010

0.00/5 (No votes)
3 Sep 2013CPOL 10.4K  
Cause: If you are passing SPWeb object in the thread method, you will get this exception.

Introduction

When you are doing coding, you may get the above exception. This exception may due to passing the SPWeb instance to a thread method. 

Using the code

Suppose in the main thread , you have created the SPWeb object and pass this object to a thread method, like this, 

C#
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
    using (SPWeb web = site.OpenWeb())
    {

        activateThread = new Thread(delegate()
        {
            this.Activate(web);
        });
        activateThread.Start();
   }
}

Here the SPWeb object web is passing to the thread method Activate. The issue is because of this only. 

Remedy 

Don't pass the SPWeb object from main thread (Application Thread) to a thread method. Instead create the SPWeb object inside the thread method (here Activate method) and dispose the web object there. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)