-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIOHelper.cs
More file actions
50 lines (42 loc) · 1.31 KB
/
Copy pathIOHelper.cs
File metadata and controls
50 lines (42 loc) · 1.31 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Components
{
public static class IOHelper
{
public const int DefaultMaxTries = 8192;
public const int DefaultSleepTime = 10;
public static void TryAction(Action IOAction)
{
TryAction(IOAction, DefaultMaxTries, DefaultSleepTime);
}
public static void TryAction(Action IOAction, int MaxTries, int SleepTime)
{
int tries = 0;
while (true)
{
try
{
IOAction();
return;
}
catch (IOException) { Thread.Sleep(SleepTime); }
catch (AccessViolationException) { Thread.Sleep(SleepTime); }
catch (UnauthorizedAccessException) { Thread.Sleep(SleepTime); }
tries++;
if (tries > MaxTries)
{
throw new IOException(string.Format("Failed after exceeding {0} tries.", MaxTries));
}
else if (SleepTime > 0)
{
Thread.Sleep(SleepTime);
}
}
}
}
}