forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuncUtils.cs
More file actions
63 lines (56 loc) · 1.7 KB
/
FuncUtils.cs
File metadata and controls
63 lines (56 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using ServiceStack.Logging;
namespace ServiceStack
{
public static class FuncUtils
{
private static readonly ILog Log = LogManager.GetLogger(typeof(FuncUtils));
/// <summary>
/// Invokes the action provided and returns true if no excpetion was thrown.
/// Otherwise logs the exception and returns false if an exception was thrown.
/// </summary>
/// <param name="action">The action.</param>
/// <returns></returns>
public static bool TryExec(Action action)
{
try
{
action();
return true;
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return false;
}
public static T TryExec<T>(Func<T> func)
{
return TryExec(func, default(T));
}
public static T TryExec<T>(Func<T> func, T defaultValue)
{
try
{
return func();
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return default(T);
}
#if !SL5 //No Stopwatch
public static void WaitWhile(Func<bool> condition, int millisecondTimeout, int millsecondPollPeriod = 10)
{
var timer = System.Diagnostics.Stopwatch.StartNew();
while (condition())
{
System.Threading.Thread.Sleep(millsecondPollPeriod);
if (timer.ElapsedMilliseconds > millisecondTimeout)
throw new TimeoutException("Timed out waiting for condition function.");
}
}
#endif
}
}