Parallel Execution of method on Button click

Multi tool use
Parallel Execution of method on Button click
Working on threads and thread pool for the first time, We have two button clicks which has a ajax call to pass in two parameters to same controller method with different values.
Html:
<button type="button" class="ApproveBtn btn btn-theme">Approve</button>
<button type="button" class="ApproveOrdDateBtn btn btn-theme">Approve OrderDate</button>
When the first button is clicked it has a dynamic script with 20 seconds sleep time and second button has script with 5 seconds sleep time.
In the repository method i have tried both thread pooling and thread Task to run the function in parallel when we click on the two buttons at a time one after the other.
The expected output is the second button with small sleep time should run first and then the second button to display the result to users, but in my case which ever we click first is getting executed followed by next.
Code:
Public object Execute(string sScript, string sDatabase)
{
Object oResult="";
//Thread pool
var waitHandle = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem
(
new WaitCallback
(
(_) =>
{
oResult = RunScriptDemo(sScript,sDatabase);
waitHandle.Set();
}
)
);
waitHandle.WaitOne();
//Task with thread
var t = Task<object>.Factory.StartNew(() =>
{
Object oTskResult = RunScriptDemo(sScript,sDatabase);
return oTskResult;
});
oResult = t.Result;
return oResult;
}
Public object RunScriptDemo(string sScript, string sDatabase)
{
object oReturn="";
return oReturn;
}
Cheers.
1 Answer
1
Is the sleep implemented within the client or at the server? What is the purpose that a user need to click "approve" and then "approve date"?
Anyway, to run task(s) in parallel I would do it as following:
Task<MyReturnType> firstTask;
Task<MyReturnType> secondTask;
//create the tasks, but not acutal execute it
firstTask = this.DoMethod1(); // return a Task<MyReturnType> -> uses await
secondTask = this.DoMethod2(); // return a Task<MyReturnType> -> uses await
//execute it in parallel and return if all tasks are done
await Task.WhenAll(firstTask, secondTask).ConfigureAwait(false);
I think, your return type is "object".
This link might be helpful: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/how-to-make-multiple-web-requests-in-parallel-by-using-async-and-await
The Sleep is implemented in the Scripts which executes in server side and returns the value, just to check the parallel execution. The buttons are dynamic we can add any number of buttons which can be used for example to check status, order date, order request etc based on the condition in the C# script loaded.
– pavan
Jul 3 at 9:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
web requests are already executes in separate threads, if you on MVC/WebAPI
– vasily.sib
Jul 3 at 8:40