Stan
Stan

Reputation: 26511

Best way to show progress of some process on view?

I have no idea how to do that, but I think it is possible. What I want to do is show progress of some process (loop) to my view.

I'm using C# ASP.NET MVC3/RAZOR

Code example:

public ActionResult Index()
{
    for (int i = 0; i < 100; i++)
    {
        System.Threading.Thread.Sleep(100); // Simulate...
    }

    return View();
}

And in my view I just want to make simple text like 44/100.

Is it possible, and if it is then what would be the best way of achieving this?

Upvotes: 2

Views: 1341

Answers (1)

Shashi Penumarthy
Shashi Penumarthy

Reputation: 813

BNL's comment above is correct.

  1. Start your task in a different thread using the Task Parallel library. Update task progress within this task.
  2. Write an action method that polls the progress every few seconds using ajax.
  3. Update your UI based on progress.

How do you keep track of progress?

  1. Create a task identifier in a database (say a table with 2 columns: a guid and a progress value).
  2. Return the task identifier value from the action method.
  3. Send the task identifier in your ajax call so you can tell the server to give you the progress of the specified task.

And yes, it's worth repeating: don't run long tasks in the web server thread. Run a windows service and let it run the tasks for you.

Upvotes: 1

Related Questions