-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (121 loc) · 4.66 KB
/
Program.cs
File metadata and controls
137 lines (121 loc) · 4.66 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace baal2000
{
/// <summary>
/// This program demonsrates a fatal CLR error (AccessViolationException) in Reflection Invoke.
/// See README.md for more details.
/// </summary>
public class Program
{
static void Main(string[] _)
{
Console.WriteLine("The test started.");
Console.WriteLine("Progress:");
ThreadPool.GetMinThreads(out int _, out int cptMin);
ThreadPool.SetMinThreads(3500, cptMin);
Test.Run();
Console.WriteLine($"Finished successfully, can Press Any Key now and then Try Again");
Console.ReadKey();
}
class Test
{
readonly TestCore methods;
MethodInfo methodInfo;
public Test()
{
methods = new TestCore();
methodInfo = GetMethod("ExceptionDispatchInfoCaptureThrow");
if (methodInfo is null)
{
throw new InvalidOperationException("The methodInfo object is missing or empty.");
}
}
public static void Run()
{
long progress = 0;
var test = new Test();
const int MaxCount = 1000000;
Parallel.For(
0,
MaxCount,
new ParallelOptions() { MaxDegreeOfParallelism = 3000 },
i =>
{
if (Interlocked.Increment(ref progress) % 10000 == 0)
{
Console.WriteLine($"{DateTime.Now} : {progress * 100D / MaxCount:000.0}%");
}
test.Invoke();
});
}
/// <summary>
/// Invokes the method using Reflection .
/// </summary>
public void Invoke()
{
try
{
methodInfo.Invoke(methods, null);
// As a workaround, try:
// methodInfo.Invoke(methods, BindingFlags.DoNotWrapExceptions, null, null, System.Globalization.CultureInfo.InvariantCulture);
}
catch
{
// Ignore
}
}
static MethodInfo GetMethod(string methodName)
{
foreach (MethodInfo method in typeof(TestCore).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (methodName == method.Name)
{
return method;
}
}
return null;
}
class TestCore
{
const int ExpirySeconds = 10;
// An exception instance that gets refreshed every ExpirySeconds.
(Exception Exception, DateTime CacheExpiryDateTime) exceptionCache;
/// <summary>
/// Captures and throws the a cached instance of an exception.
/// </summary>
public void ExceptionDispatchInfoCaptureThrow()
{
var error = GetCachedError();
// Looks to be not thread-safe when the same error is captures and thrown, then gets wrapped into TargetInvocationException by System.RuntimeMethodHandle.InvokeMethod.
ExceptionDispatchInfo.Capture(error).Throw();
// As a workaround, try:
// throw new Exception("", error);
}
Exception GetCachedError()
{
try
{
var cache = exceptionCache;
if (cache.Exception != null)
{
if (exceptionCache.CacheExpiryDateTime > DateTime.UtcNow)
{
return cache.Exception;
}
}
throw new Exception("Test");
}
catch (Exception ex)
{
exceptionCache = (ex, DateTime.UtcNow.AddSeconds(ExpirySeconds));
return ex;
}
}
}
}
}
}