1

hi to all i am trying to get a access a folder in my thread like this

protected string GetFolderName(int OrgID)
{
    string FolderName = string.Empty;
    string[] strDirectories = Directory.GetDirectories(HttpContext.Current.Server.MapPath("~/Uploads/"));
    if (strDirectories.Length > 0)
    {
        for (int count = 0; count < strDirectories.Length; count++)
        {
            string name = strDirectories[count].Substring(strDirectories[count].LastIndexOf("\\") + 1);
            if (name.Contains("_"))
            {
                string companyId = name.Substring(name.LastIndexOf("_") + 1);
                if (Convert.ToInt32(companyId) == OrgID)
                {
                    FolderName = name;
                    break;
                }
            }
        }
    }
    return FolderName;
}

This method is invoked through the thread pool and it's giving me an error of "Object reference not set to an instance of an object" on this line

string[] strDirectories = Directory.GetDirectories(HttpContext.Current.Server.MapPath("~/Uploads/"));

Please help me out


Solution

i used HttpRuntime.AppDomainAppPath in place of HttpContext.Current.Server.MapPath and it works perfectly.

2 Answers 2

2

HttpContext.Current returns the current context in the executing thread. The thread pool thread isn't responsible for any HTTP request, so it doesn't have a context. The Current property will return null, which is why you're getting the exception. I suggest you call Directory.GetDirectories() before you transfer to the thread pool. An alternative would be to pass the context instead.

EDIT: If you don't want to perform Directory.GetDirectories() in the thread pool thread, you could at least evaluate HttpContext.Current.Server.MapPath("~/Uploads/") in the original thread, and make that available to the thread pool. Basically you just want to avoid evaluating HttpContext.Current on the wrong thread.

5
  • thanks for reply @Jon Skeet but i can't use Directory.GetDirectories() before transfer to thread pool because i have to do some functionality in their and other operation like file.move also
    – rahul
    Sep 29, 2012 at 9:56
  • @rahul: Why? That doesn't sound like a good reason to me. After all, it's basically the first thing you do in the thread pool thread - so why not do it just beforehand?
    – Jon Skeet
    Sep 29, 2012 at 10:29
  • actually this is just a part of that thread pool original code is too long to paste here
    – rahul
    Sep 29, 2012 at 10:36
  • @rahul: It would have been helpful if you could have explained that before. Well, yet another alternative is to evaluate HttpContext.Current.Server.MapPath("~/Uploads/") in the original thread, and pass that down to the thread pool thread. I'll edit my post.
    – Jon Skeet
    Sep 29, 2012 at 10:37
  • yes i am doing the same thing now and i am using this HttpRuntime.AppDomainAppPath in place of Server.MapPath thanks for ur support
    – rahul
    Sep 29, 2012 at 10:40
1

Send HttpContext.Current.Server.MapPath("~/Uploads/") as the last parameter in QueueUserWorkItem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.