1

There is an operation in a program that will take up to 'n' times longer than another operation (so I need to wait 'n' times longer before failing). The timeout for the other operation is saved as a TimeSpan. How do I get (OtherOperation.TimeSpan * 'n')?

wonea
  • 4,783
  • 17
  • 86
  • 139
Rufus L
  • 36,127
  • 5
  • 30
  • 43

2 Answers2

1

You might consider writing an extension method, such as:

public static class TimeSpanEx
{
    public static TimeSpan MultiplyBy (this TimeSpan t, int multiplier)
    {
        return new TimeSpan(t.Ticks * multiplier);
    }
}

Now you can simply call:

TimeSpan result = yourtimespan.MultiplyBy(3);

As an aside, it would be really nice if one could just overload the * operator in an extension method, but this is currently not possible.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

Are we talking .net?

If so use create a new timespan using timespan.frommilliseconds and pass in the original .totalmilliseconds value multiplied by your constant.

Hope that helps

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • 1
    Thanks, using your suggestion except ticks instead of milliseconds: `TimeSpan timeout = TimeSpan.FromTicks(otherProcess.TimeOut.Ticks * multiplier);` – Rufus L Sep 03 '14 at 22:29