Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:43:42+00:00 2026-05-30T10:43:42+00:00

WEB applications based on. NET MVC 3 to establish the C # asynchronous operating

  • 0

WEB applications based on. NET MVC 3 to establish the C # asynchronous operating value error, code as follows:

public ActionResult Contact ()
            {
                    / / Create an asynchronous processing operations
                    Task task = new Task (() => {
                            string [] testTexts = new string [10] {"a", "b", "c", "d", "e", "f", "g", "h" , "i", "j"};
                            foreach (string text in testTexts)
                            {
                                    / / The following line does not have a problem
                                    System.IO.File.AppendAllText (Server.MapPath ("/ test.txt"), text);
                                    / / The following line to be a problem, find a solution. Because some other program of practical application in my project set to use System.Web.HttpContext.Current
                                    / / System.IO.File.AppendAllText (System.Web.HttpContext.Current.Server.MapPath ("/ test.txt"), text);
                                    / / Thread to hang five seconds to simulate asynchronous time difference
                                    System.Threading.Thread.Sleep (5000);
                            }
                    });
                    / / Asynchronous processing
                    task.Start ();
                    return View ();
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:43:43+00:00Added an answer on May 30, 2026 at 10:43 am

    Since the HttpContext is bound to the current request, once you return it will no longer be available. But your asynchronous task continues to run in the background and when it attempts to access it, it is no longer available. For this reason you should pass all dependencies to the task as a parameter:

    public ActionResult Contact()
    {
        // everything that depends on an HttpContext should be done here and passed
        // as argument to the task
        string p = HttpContext.Server.MapPath("~/test.txt");
    
        // Create an asynchronous processing operations
        Task task = new Task(state =>
        {
            var path = (string)state;
            var testTexts = new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
            foreach (string text in testTexts)
            {
                System.IO.File.AppendAllText(path, text);
    
                // Thread to hang five seconds to simulate asynchronous time difference
                Thread.Sleep(5000);
            }
        }, p);
    
        // Asynchronous processing
        task.Start();
        return View();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an application which is web-based (asp.net MVC). The user configures reminders
I'm developing a web application based on ASP.NET MVC. There are a large database.
We have a web based (ASP.NET MVC) application that uses SQL Server 2008 for
I'm developing a web application in ASP.NET MVC with NHibernate. Based in articles and
We are in the process of developing a .NET based IIS hosted web application
I'm building an ASP .NET 2.0 (C#) based web application, which is primarily intended
I’ve got a situation with a large internal corporate web based application running ASP.NET
Let me share the following scenario: I have a ASP.NET intranet Web-based application that
I am working with a Web application, based on Asp.Net 3.5 and WSS 3.0
I have a code base that has been used as an ASP.Net web application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.