Oguz Karadenizli
Oguz Karadenizli

Reputation: 3505

How can I get TaskExecutionTime in C# Task Parallel Library?

I'm using Task Library and using Wait with timeout parameter. How can I set taskExecutionTime?

int taskExecutionTime=5000;
try
{
    bool hasTimeout=!myTask.Wait(5000);
    if (!hasTimeout)
       taskExecutionTime=???     
}
catch (AggregateException)
{
    taskExecutionTime=???
}

UPDATE:

I don't need real time performance of myTask. For example I'm using .NET's HtpWebRequest with different proxies. I want to know and compare proxy performances. Generic solution will be better, so everyone of us can use it.

Upvotes: 0

Views: 701

Answers (1)

James Manning
James Manning

Reputation: 13589

Since you're doing a Wait call, I would think you would just time it the normal way you would synchronous code - Stopwatch.StartNew() or var startTime = DateTime.Now or whatever right when you start the task, then look at the elapsed time when the Wait returns false or throws.

Am I misunderstanding what you're trying to do?

Upvotes: 1

Related Questions