Note: I am restricted to .net 4.0
I am currently using stopwatch to measure the time it takes for an action to run. After the action completes, I do logic based on the time. More specificaly, I check if the time went beyond a certain threshold.
I recently discovered the Task.Wait(int timeout) method
Is it better to put my action in a task and wait for it on my threshold?
for example:
bool isOnTime=false;
Task action = new Task(() => someMethod());
action.Start();
isOnTime = action.Wait(myThreshold);
if(!isOnTime)
{
...some code
}
Edit: Updating the question since everyone thinks i want to run a task
This is how i currently do it:
Stopwatch watch = Stopwatch.StartNew();
someMethod();
watch.Stop();
executeTime = watch.ElapsedMilliseconds;
later i am running:
if (executeTime > Threshold)
I am running the method synchronously and i want to keep it that way