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 8227229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:03:19+00:00 2026-06-07T16:03:19+00:00

How do I execute an asynchronous method from within a controller method and return

  • 0

How do I execute an asynchronous method from within a controller method and return an HttpStatusCodeResult(200), without the async delegate prematurely terminating its execution?

I am working on an asp.net application and one of my action my home controller takes long time to run(10-30 sec). I want to return an HttpStatusCodeResult(200) and keep running my function. The functoin need not to return anything, but still it’s not really a case of fire and forget since I return a response to the server right away with HttpStatusCodeResult.

I tried using delegates, but it seems once I return the status code from the action, the delegate stops executing. Another option is to create a windows service but that would be like using a bazooka to kill a fly. I get very few requests so resources and performance is not an issue in my case. I heard ThreadPool.QueueUserWorkItem is an option, but which is most suitable for my case?

  • 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-06-07T16:03:22+00:00Added an answer on June 7, 2026 at 4:03 pm

    Create a static class that manages this long-running operation. This class will be responsible for creating threads to perform the task and also provides a way to check up on the status of any ongoing operations (i.e. if it’s still in progress or has finished, and if so, what the result is).

    Your MVC Controller method then uses this class to do the work and present status information or the completed processed data to the user.

    Something like this:

    public ActionResult GetSomething(String id) {
        TaskResult result = TaskClass.GetStatus( id );
        if( result == null ) { // the task has not been run, so start it up
            TaskClass.StartNew( id );
            return new View("PleaseWait");
        } else if( !result.IsFinished ) {
            return new View("PleaseWait");
        } else {
            return View("Results", result);
        }
    }
    
    public static class TaskClass {
         private static Dictionary<String,TaskResult> _tasks;
         public static TaskResult GetStatus(String id) {
             // TODO: Make this code thread-safe
             if( _tasks.ContainsKey(id) ) return _tasks[id];
             return null;
         }
         public static void Start(String id) {
             _tasks.Add( id, new TaskResult("Working") );
             Thread thread = new Thread( SomeExpensiveOperation );
             thread.Start( id );
         }
    }
    

    At the end of SomeExpensiveOperation there would be code that marks the TaskResult as finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a particularly long running method that I need to execute from my
I have an asynchronous method in my EJB singleton that's called from another method
I want to execute an asynchronous callback when exit event is emitted. For example:
Are public events asynchronous? Do they execute on a separate thread?
I execute some tests within my application, but before doing that I have to
cursor.execute(SELECT user_id FROM myapp_location WHERE\ GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s\ ,(user_utm_easting, user_utm_northing, 500)); This
I'm trying to convert a form from synchronous to asynchronous using the Ajax.BeginForm helper
I need to execute a bunch of asynchronous methods (client SQLite database), and call
I'm struggling to figure out why an @Asynchronous method in my EJB isn't actually
I noticed that code after return from ExecuteRegisteredAsyncTasks call is executed in same thread

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.