Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I am having web service reference in my windows application and this web service having many methods . I want to set the different time out for different methods.
for e.g methodA set TimeOut = 10 sec
methodB set TimeOut = 20 sec

Thanks in advance :)

What I have tried:

C#
Hi ,

I am having web service reference in my windows application  and this web service having many methods . I want to set the different  time out for different methods.
for e.g  methodA set TimeOut = 10 sec
         methodB set TimeOut = 20 sec

Thanks in advance :)
Posted
Updated 13-Oct-16 3:16am
v2

1 solution

You can't set a timeout on a per-method basis, but could do something like what is described at this link:

Crafting a Task.TimeoutAfter Method | Parallel Programming with .NET[^]

My google search was "c# timeout with task".

Here's some minimal code that may work for you:

C#
/// <summary>
/// Times out if the task execution takes longer that the specified number of milliseconds.
/// Up to 596.5 hours of timeout can be specified.
/// </summary>
/// <param name="task">The task to execute</param>
/// <param name="msTimeout">How long to wait for the task to return (&lt;= 0 causes
/// this value to be = int.MaxValue.</param>
/// <returns></returns>
public static async Task TimeoutAfter(this Task task, int msTimeout)
{
    // make sure we have a sane timout value
    msTimeout = (msTimeout <= 0) ? int.MaxValue : msTimeout;
    if (task == await Task.WhenAny(task, Task.Delay(msTimeout)))
    {
        await task;
    }
    else
    {
        throw new TimeoutException();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900