2

I have a stored procedure which can run dynamic query and produce a result set to static table. which in return will look like below (conceptually) enter image description here

I can display result to the user via SQL Dependency or node.js but I want to understand how exactly I can execute same stored procedure multiple times and wait for the execution for response?

Let say function which is calling SP is fnCallSPForDynamicExec Will calling this function multiple times (using threading) do the justice for me. Something like this

 Page_Load()
    {

       foreach(var task in tasklist)
       {
        Task taskWork = Task.Run(new Action(fnCallSPForDynamicExec(task.taskid));
             // Do something on UI
        taskWork.Wait();
             // Do something on UI
       }
    }


    private void fnCallSPForDynamicExec (int task id)
    {
        //call dynamic SP
    }

Is this correct or any other way to achieve this?

I would appreciate if you can redirect me to some blog/white paper/sample which has done this kind of job

P.S.: Above code is just based on assumption, it may be not be correct in terms of syntax.

Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206

1 Answers1

0

You are on the right track, but do not use Task.Wait() as that can result in a deadlock. See Best Practices in Asynchronous Programming. You can just await the call to Task.Run.

public void async Page_Load()
 {
    foreach(var task in tasklist)
    {
        await Task.Run(() => fnCallSPForDynamicExec(task.taskid));
            // Do something on UI   
    }
 }

private void fnCallSPForDynamicExec (int taskid)
{
   //call dynamic SP
}
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61