0

I called method that need 2 minutes or more to complete

I added ProgressPercentproperty to get current progress state.

I want to read percent by angularjs and show to user this is my code only for method . I do not write angular code yet

    public void CreatePDF()
    {
      //huge work here
      for(int i=0 , i < n, i++)
       {
        //
        ProgressProperty += 0.001;
       }
    }

The question is : How I can read ProgressProperty while the process is running and run process in background;

Thank you

1 Answers1

0

Monitor Server Tasks with ASP.NET SignalR and Bootstrap

The Lengthy method on TaskController is the classic, potentially lengthy, task that may take a while to complete and for which notifications to the client are appropriate and convenient for the user. For the sake of the demo, here’s some dummy code that just simulates the step-by-step execution of a typical business workflow.

public class TaskController : Controller
{   public void Lengthy([Bind(Prefix="id")] string taskId)
{
    ProgressHub.NotifyStart(taskId);

    // A bit of work
    Thread.Sleep(2000);
    ProgressHub.NotifyProgress(taskId, 20);

    // A bit of work        
    Thread.Sleep(1000);        
    ProgressHub.NotifyProgress(taskId, 30);

    // A bit of work
    Thread.Sleep(2000);
    ProgressHub.NotifyProgress(taskId, 50);

    Thread.Sleep(3000);
    ProgressHub.NotifyProgress(taskId, 80);

    // A bit of work
    Thread.Sleep(1000);
    ProgressHub.NotifyProgress(taskId, 90);

    // Final piece of work
    Thread.Sleep(1000);
    ProgressHub.NotifyEnd(taskId);
}
}

As you can see, the task is articulated in several steps, each of which marks an amount of work done. To keep things as general as possible I envisioned a programming API made of three methods: NotifyStart, NotifyProgress and NotifyEnd. All these methods must be defined in the ASP.NET SignalR hub class. Here’s a possible implementation of the hub class.

public class ProgressHub : Hub
{
public void NotifyStart(string taskId)
{
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
    hubContext.Clients.All.initProgressBar(taskId);
}
public void NotifyProgress(string taskId, int percentage)
{
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
    hubContext.Clients.All.updateProgressBar(taskId, percentage);
}
public void NotifyEnd(string taskId)
{
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
    hubContext.Clients.All.clearProgressBar(taskId);
}
}