@@ -99,7 +99,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>
99
99
100
100
public static Action < Exception > ? OnInvokeException { get ; set ; }
101
101
102
- private static readonly Lock _exitLock = new ( ) ;
102
+ private static readonly Lock ExitLock = new ( ) ;
103
103
private static volatile bool _exited ;
104
104
private static readonly Lazy < CancellationTokenSource > LazyCancellationTokenSource = new ( ) ;
105
105
private static void InvokeExitHandler ( object ? sender , EventArgs ? args )
@@ -116,7 +116,7 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
116
116
// posixSignalContext.Cancel = true;
117
117
// }
118
118
// #endif
119
- lock ( _exitLock )
119
+ lock ( ExitLock )
120
120
{
121
121
if ( _exited ) return ;
122
122
Debug . WriteLine ( "exiting..." ) ;
@@ -130,105 +130,148 @@ private static void InvokeExitHandler(object? sender, EventArgs? args)
130
130
}
131
131
}
132
132
133
- [ Obsolete ( "Please use ApplicationHelper.ExitToken instead" , true ) ]
134
- public static CancellationToken GetExitToken ( ) => GetExitTokenInternal ( ) ;
135
-
136
133
internal static CancellationToken GetExitTokenInternal ( ) => LazyCancellationTokenSource . Value . Token ;
137
134
138
- public static void TryInvoke ( Action action )
135
+ public static void TryInvoke ( Action action , int ? maxRetryCount = null )
139
136
{
140
- Guard . NotNull ( action , nameof ( action ) ) ;
137
+ Guard . NotNull ( action ) ;
138
+
139
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
140
+ invoke :
141
+
141
142
try
142
143
{
143
144
action ( ) ;
144
145
}
145
146
catch ( Exception ex )
146
147
{
147
148
OnInvokeException ? . Invoke ( ex ) ;
149
+ if ( maxRetry -- > 0 )
150
+ goto invoke ;
148
151
}
149
152
}
150
153
151
- public static void TryInvoke < T > ( Action < T > action , T t )
154
+ public static void TryInvoke < T > ( Action < T > action , T t , int ? maxRetryCount = null )
152
155
{
153
- Guard . NotNull ( action , nameof ( action ) ) ;
156
+ Guard . NotNull ( action ) ;
157
+
158
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
159
+ invoke :
160
+
154
161
try
155
162
{
156
163
action ( t ) ;
157
164
}
158
165
catch ( Exception ex )
159
166
{
160
167
OnInvokeException ? . Invoke ( ex ) ;
168
+ if ( maxRetry -- > 0 )
169
+ goto invoke ;
161
170
}
162
171
}
163
172
164
- public static void TryInvoke < T1 , T2 > ( Action < T1 , T2 > action , T1 t1 , T2 t2 )
173
+ public static void TryInvoke < T1 , T2 > ( Action < T1 , T2 > action , T1 t1 , T2 t2 , int ? maxRetryCount = null )
165
174
{
166
- Guard . NotNull ( action , nameof ( action ) ) ;
175
+ Guard . NotNull ( action ) ;
176
+
177
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
178
+ invoke :
179
+
167
180
try
168
181
{
169
182
action ( t1 , t2 ) ;
170
183
}
171
184
catch ( Exception ex )
172
185
{
173
186
OnInvokeException ? . Invoke ( ex ) ;
187
+ if ( maxRetry -- > 0 )
188
+ goto invoke ;
174
189
}
175
190
}
176
191
177
- public static async Task TryInvokeAsync < T1 , T2 > ( Func < T1 , T2 , Task > func , T1 t1 , T2 t2 )
192
+ public static async Task TryInvokeAsync < T1 , T2 > ( Func < T1 , T2 , Task > func , T1 t1 , T2 t2 , int ? maxRetryCount = null )
178
193
{
179
- Guard . NotNull ( func , nameof ( func ) ) ;
194
+ Guard . NotNull ( func ) ;
195
+
196
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
197
+ invoke :
198
+
180
199
try
181
200
{
182
201
await func ( t1 , t2 ) ;
183
202
}
184
203
catch ( Exception ex )
185
204
{
186
205
OnInvokeException ? . Invoke ( ex ) ;
206
+ if ( maxRetry -- > 0 )
207
+ goto invoke ;
187
208
}
188
209
}
189
210
190
- public static void TryInvoke < T1 , T2 , T3 > ( Action < T1 , T2 , T3 > action , T1 t1 , T2 t2 , T3 t3 )
211
+ public static void TryInvoke < T1 , T2 , T3 > ( Action < T1 , T2 , T3 > action , T1 t1 , T2 t2 , T3 t3 , int ? maxRetryCount = null )
191
212
{
192
- Guard . NotNull ( action , nameof ( action ) ) ;
213
+ Guard . NotNull ( action ) ;
214
+
215
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
216
+ invoke :
217
+
193
218
try
194
219
{
195
220
action ( t1 , t2 , t3 ) ;
196
221
}
197
222
catch ( Exception ex )
198
223
{
199
224
OnInvokeException ? . Invoke ( ex ) ;
225
+ if ( maxRetry -- > 0 )
226
+ goto invoke ;
200
227
}
201
228
}
202
229
203
- public static async Task TryInvokeAsync ( Func < Task > func )
230
+ public static async Task TryInvokeAsync ( Func < Task > func , int ? maxRetryCount = null )
204
231
{
205
- Guard . NotNull ( func , nameof ( func ) ) ;
232
+ Guard . NotNull ( func ) ;
233
+
234
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
235
+ invoke :
236
+
237
+
206
238
try
207
239
{
208
240
await func ( ) ;
209
241
}
210
242
catch ( Exception ex )
211
243
{
212
244
OnInvokeException ? . Invoke ( ex ) ;
245
+ if ( maxRetry -- > 0 )
246
+ goto invoke ;
213
247
}
214
248
}
215
249
216
- public static async Task TryInvokeAsync < T > ( Func < T , Task > func , T t )
250
+ public static async Task TryInvokeAsync < T > ( Func < T , Task > func , T t , int ? maxRetryCount = null )
217
251
{
218
- Guard . NotNull ( func , nameof ( func ) ) ;
252
+ Guard . NotNull ( func ) ;
253
+
254
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
255
+ invoke :
256
+
219
257
try
220
258
{
221
259
await func ( t ) ;
222
260
}
223
261
catch ( Exception ex )
224
262
{
225
263
OnInvokeException ? . Invoke ( ex ) ;
264
+ if ( maxRetry -- > 0 )
265
+ goto invoke ;
226
266
}
227
267
}
228
268
229
- public static async Task TryInvokeAsync < T1 , T2 , T3 > ( Func < T1 , T2 , T3 , Task > func , T1 t1 , T2 t2 , T3 t3 )
269
+ public static async Task TryInvokeAsync < T1 , T2 , T3 > ( Func < T1 , T2 , T3 , Task > func , T1 t1 , T2 t2 , T3 t3 , int ? maxRetryCount = null )
230
270
{
231
- Guard . NotNull ( func , nameof ( func ) ) ;
271
+ Guard . NotNull ( func ) ;
272
+
273
+ var maxRetry = maxRetryCount . GetValueOrDefault ( ) ;
274
+ invoke :
232
275
233
276
try
234
277
{
@@ -237,6 +280,9 @@ public static async Task TryInvokeAsync<T1, T2, T3>(Func<T1, T2, T3, Task> func,
237
280
catch ( Exception ex )
238
281
{
239
282
OnInvokeException ? . Invoke ( ex ) ;
283
+
284
+ if ( maxRetry -- > 0 )
285
+ goto invoke ;
240
286
}
241
287
}
242
288
0 commit comments