private void TimerProcessAction ( int times , Action <EasyTimer> timerAction , Func <EasyTimer, Exception, Exception, string, bool> onCaughtExceptionProcessFunc ) { try { if (timerAction == null) { return; } if (_timer != null) { lock (_locker) { _timer.Stop(); _timer.Enabled = false; } } DateTime begin; do { begin = DateTime.Now; TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { timerAction(this); } , false , (x, y, w) => { var reThrowException = false; if (onCaughtExceptionProcessFunc != null) { reThrowException = onCaughtExceptionProcessFunc(this, x, y, w); } return(reThrowException); } , null ); } while (Math.Abs(DateTimeHelper.SecondsDiffNow(begin)) > times * _intervalInSeconds); } finally { if (_timer != null) { lock (_locker) { _timer.Enabled = true; _timer.Start(); } } } }
private void SocketReceiveFromAsyncAndSyncCompleteProcess ( SocketAsyncEventArgs receiveSocketAsyncEventArgs , Func < SocketAsyncDataHandler <T> , EndPoint , byte[] , SocketAsyncEventArgs , bool //是否继续收 > onDataReceivedProcessFunc , Func < SocketAsyncDataHandler <T> , SocketAsyncEventArgs , Exception , bool //是否Rethrow > onCaughtExceptionProcessFunc , int sleepInMilliseconds = 10 ) { TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { while (!_socket.ReceiveFromAsync(receiveSocketAsyncEventArgs)) { if (receiveSocketAsyncEventArgs.BytesTransferred > 0) { ProcessReceivedData ( _socket , ReceiveSocketAsyncEventArgs , onDataReceivedProcessFunc , onCaughtExceptionProcessFunc ); } else { if (sleepInMilliseconds > 0) { Thread.Sleep(sleepInMilliseconds); } } } } , false , (x, y) => { return(onCaughtExceptionProcessFunc(this, receiveSocketAsyncEventArgs, x)); } ); }
private static bool TryGetRefreshedCacheData ( Func <TData> onRefreshingCacheDataProcessFunc , out TData refreshedData , bool needTryProcess = true , bool reThrowException = false , Func <Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onFinallyProcessAction = null ) { var r = false; TData refreshingData = default(TData); refreshedData = default(TData); if (needTryProcess) { TryCatchFinallyProcessHelper .TryProcessCatchFinally ( needTryProcess , () => { refreshingData = onRefreshingCacheDataProcessFunc(); r = true; } , reThrowException , (xx, yy, zz) => { return (onCaughtExceptionProcessFunc(xx, yy, zz)); } ); if (r) { refreshedData = refreshingData; } } else { refreshedData = onRefreshingCacheDataProcessFunc(); r = true; } return(r); }
public static T ProcessWaitingShowDialog <T> ( IWin32Window ownerWindow , Form dialogForm , Func <T> onProcessFunc , out DialogResult dialogResult , Func <Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onFinallyProcessAction = null ) { T r = default(T); dialogResult = default(DialogResult); var IsCompleted = false; new Thread ( new ThreadStart ( () => { //wait.WaitOne(); Thread.Sleep(10); TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { r = onProcessFunc(); IsCompleted = true; } , false , onCaughtExceptionProcessFunc , onFinallyProcessAction ); } ) ).Start(); if (!IsCompleted) { dialogResult = dialogForm.ShowDialog(ownerWindow); } return(r); }
private void DequeueProcess ( Action <long, T> onOnceDequeueProcessAction = null , int sleepInMilliseconds = 100 , Action <long, List <Tuple <long, T> > > onBatchDequeuesProcessAction = null , int waitOneBatchTimeOutInMilliseconds = 1000 , int waitOneBatchMaxDequeuedTimes = 100 , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction = null , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction = null ) { if (_isStartedDequeueProcess) { return; } List <Tuple <long, T> > list = null; long i = 0; Stopwatch stopwatch = null; if (onBatchDequeuesProcessAction != null) { list = new List <Tuple <long, T> >(); stopwatch = new Stopwatch(); stopwatch.Start(); } while (true) { TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { if (!_queue.IsEmpty) { Tuple <Stopwatch, T> element = null; if (_queue.TryDequeue(out element)) { var enabledCountPerformance = true; { if (OnGetEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc(); } } var qpcc = _queuePerformanceCountersContainer; Stopwatch stopwatchDequeue = null; var stopwatchEnqueue = element.Item1; if (enabledCountPerformance) { stopwatchDequeue = _stopwatchsPool.Get(); } _timerCounters[0].Item2 = stopwatchEnqueue; _timerCounters[1].Item2 = stopwatchDequeue; #region while queue.IsEmpty loop var reThrowException = false; PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , qpcc.IncrementCountersBeforeCountPerformanceForDequeue , qpcc.DecrementCountersBeforeCountPerformanceForDequeue , _timerCounters , () => //try { if (onOnceDequeueProcessAction != null) { var item = element.Item2; onOnceDequeueProcessAction (i, item); } } , (x, y, z) => //catch { qpcc .CaughtExceptionsPerformanceCounter .Increment(); z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z; if (onDequeueProcessCaughtExceptionProcessFunc != null) { reThrowException = onDequeueProcessCaughtExceptionProcessFunc ( x , y , z ); } else if (OnCaughtException != null) { reThrowException = OnCaughtException(this, x, y, z); } return(reThrowException); } , onDequeueProcessFinallyProcessAction //finally , null , qpcc.IncrementCountersAfterCountPerformanceForDequeue ); #endregion while queue.IsEmpty loop //池化 if (stopwatchEnqueue != null) { stopwatchEnqueue.Reset(); var r = _stopwatchsPool.Put(stopwatchEnqueue); if (!r) { stopwatchEnqueue.Stop(); stopwatchEnqueue = null; } } if (stopwatchDequeue != null) { stopwatchDequeue.Reset(); var r = _stopwatchsPool.Put(stopwatchDequeue); if (!r) { stopwatchDequeue.Stop(); stopwatchDequeue = null; } } if (onBatchDequeuesProcessAction != null) { i++; var item = element.Item2; var tuple = Tuple .Create <long, T> ( i , item ); list.Add(tuple); } } } else { if (sleepInMilliseconds > 0) { Thread.Sleep(sleepInMilliseconds); } } if (onBatchDequeuesProcessAction != null) { if ( i >= waitOneBatchMaxDequeuedTimes || stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds ) { if (i > 0) { if (stopwatch != null) { stopwatch.Stop(); } EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .TryCountPerformance ( PerformanceCounterProcessingFlagsType.All , _performanceCountersCategoryNameForBatchProcess , _performanceCountersCategoryInstanceNameForBatchProcess , () => { return(OnGetEnabledCountPerformanceProcessFunc()); } , () => { onBatchDequeuesProcessAction ( i , list ); } , null , (xx, yy, zz) => { var rrr = false; zz = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz; if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null) { rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc ( xx, yy, zz ); } else if (OnCaughtException != null) { rrr = OnCaughtException(this, xx, yy, zz); } return(rrr); } , (xx, yy, zz, ww) => { i = 0; list.Clear(); if (stopwatch != null) { stopwatch.Restart(); } onDequeuesBatchProcessFinallyProcessAction? .Invoke(xx, yy, zz, ww); } ); } } } } , false , (x, y, z) => { var rr = false; if (OnCaughtException != null) { rr = OnCaughtException ( this, x, y, z ); } return(rr); } ); } }
//[Conditional("NETFRAMEWORK4_X")] public static void RegisterHttpClientProgressMessageHandler ( Action <HttpClient> onHttpClientSendReceiveProcessAction , Action < HttpClient , bool , HttpProgressEventArgs > onProgressProcessAction = null , bool reThrowException = false , Func <Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onFinallyProcessAction = null ) { HttpClientHandler httpClientHandler = null; HttpClient httpClient = null; ProgressMessageHandler progressMessageHandler = null; EventHandler <HttpProgressEventArgs> httpSendProgress = null; EventHandler <HttpProgressEventArgs> httpReceiveProgress = null; var needProgress = false; try { if (onProgressProcessAction != null) { needProgress = true; httpClientHandler = new HttpClientHandler(); progressMessageHandler = new ProgressMessageHandler(httpClientHandler); httpSendProgress = new EventHandler <HttpProgressEventArgs> ( (sender, httpProgressEventArgs) => { onProgressProcessAction ( httpClient , true , httpProgressEventArgs ); } ); httpReceiveProgress = new EventHandler <HttpProgressEventArgs> ( (sender, httpProgressEventArgs) => { onProgressProcessAction ( httpClient , false , httpProgressEventArgs ); } ); progressMessageHandler.HttpSendProgress += httpSendProgress; progressMessageHandler.HttpReceiveProgress += httpReceiveProgress; } using ( httpClient = (needProgress ? new HttpClient(progressMessageHandler) : new HttpClient()) ) { TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { onHttpClientSendReceiveProcessAction(httpClient); } , false , (exception, newException, message) => { if (onCaughtExceptionProcessFunc != null) { reThrowException = onCaughtExceptionProcessFunc ( exception , newException , message ); } if (reThrowException) { throw newException; } return(false); } , (caughtException, Exception, newException, message) => { onFinallyProcessAction? .Invoke ( caughtException , Exception , newException , message ); } ); } } finally { if (progressMessageHandler != null) { progressMessageHandler.HttpSendProgress -= httpSendProgress; progressMessageHandler.HttpReceiveProgress -= httpReceiveProgress; progressMessageHandler.Dispose(); } if (httpClientHandler != null) { httpClientHandler.Dispose(); } } }
public static void TryChangeAverageTimerCounterValue ( this PerformanceCounter performanceCounter , PerformanceCounter basePerformanceCounter , Stopwatch stopwatch , Func <bool> onEnabledCountPerformanceProcessFunc = null , Action onCountPerformanceInnerProcessAction = null , Func <PerformanceCounter, Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null , Action <PerformanceCounter, PerformanceCounter, bool, Exception, Exception, string> onFinallyProcessAction = null ) { //Stopwatch stopwatch = null; var enabledCountPerformance = true; if (onEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = onEnabledCountPerformanceProcessFunc(); } if (enabledCountPerformance) { //stopwatch.Reset(); //stopwatch.Start(); stopwatch.Restart(); } if (onCountPerformanceInnerProcessAction != null) { bool reThrowException = false; TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { onCountPerformanceInnerProcessAction(); } , reThrowException , (x, y, z) => { var r = reThrowException; if (onCaughtExceptionProcessFunc != null) { r = onCaughtExceptionProcessFunc ( performanceCounter , x , y , z ); } return(r); } , (x, y, z, w) => { if ( enabledCountPerformance ) { CountEndAverageTimerCounter ( performanceCounter , basePerformanceCounter , stopwatch ); } if (onFinallyProcessAction != null) { onFinallyProcessAction ( performanceCounter , basePerformanceCounter , x , y , z , w ); } } ); } }
public bool StartReceiveDataFrom ( EndPoint remoteEndPoint , SocketAsyncEventArgs receiveSocketAsyncEventArgs , Func < SocketAsyncDataHandler <T> , EndPoint , byte[] , SocketAsyncEventArgs , bool //是否继续收 > onDataReceivedProcessFunc , Func < SocketAsyncDataHandler <T> , SocketAsyncEventArgs , Exception , Exception , string , bool //是否Rethrow > onCaughtExceptionProcessFunc = null ) { if (!_isStartedReceiveData) { //ReceiveSocketAsyncEventArgs = receiveSocketAsyncEventArgs; //ReceiveSocketAsyncEventArgs.RemoteEndPoint = remoteEndPoint; _receiveSocketAsyncEventArgsCompletedEventHandlerProcessAction = new EventHandler <SocketAsyncEventArgs> ( (sender, e) => { var socket = sender as Socket; //2015-11-13 TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { ProcessReceivedData ( socket , e , onDataReceivedProcessFunc , onCaughtExceptionProcessFunc ); SocketReceiveFromAsyncAndSyncCompleteProcess ( receiveSocketAsyncEventArgs , onDataReceivedProcessFunc , onCaughtExceptionProcessFunc ); } , false , (x, y, w) => { return(onCaughtExceptionProcessFunc ( this , receiveSocketAsyncEventArgs , x , y , w )); } ); } ); ReceiveSocketAsyncEventArgs .Completed += _receiveSocketAsyncEventArgsCompletedEventHandlerProcessAction; SocketReceiveFromAsyncAndSyncCompleteProcess ( receiveSocketAsyncEventArgs , onDataReceivedProcessFunc , onCaughtExceptionProcessFunc ); _isStartedReceiveData = true; } return(_isStartedReceiveData); }
private void DequeueProcess <TBatchGroupByKey> ( Func <long, JToken, bool> onOnceDequeueProcessFunc , int sleepInMilliseconds = 100 , Func <JToken, TBatchGroupByKey> keySelector = null , Action <long, bool, TBatchGroupByKey, IEnumerable <JToken> > onBatchDequeuesProcessAction = null , int waitOneBatchTimeOutInMilliseconds = 1000 , int waitOneBatchMaxDequeuedTimes = 100 , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction = null , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction = null ) { if (_isStartedDequeueProcess) { return; } List <JToken> list = null; var needBatchGroupBy = false; if (keySelector != null) { needBatchGroupBy = true; } long i = 0; Stopwatch stopwatch = null; if (onBatchDequeuesProcessAction != null) { list = new List <JToken>(); stopwatch = new Stopwatch(); stopwatch.Start(); } while (true) { TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { if (!_queue.IsEmpty) { Tuple <Stopwatch, JToken> element = null; if (_queue.TryDequeue(out element)) { var enabledCountPerformance = true; { if (OnGetEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc(); } } var qpcc = _queuePerformanceCountersContainer; Stopwatch stopwatchDequeue = null; var stopwatchEnqueue = element.Item1; if (enabledCountPerformance) { _stopwatchsPool.TryGet(out stopwatchDequeue); } _timerCounters[0].Item2 = stopwatchEnqueue; _timerCounters[1].Item2 = stopwatchDequeue; #region while queue.IsEmpty loop var reThrowException = false; JToken item = null; bool needAdd = false; PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , qpcc.IncrementCountersBeforeCountPerformanceForDequeue , qpcc.DecrementCountersBeforeCountPerformanceForDequeue , _timerCounters , () => //try { if (onOnceDequeueProcessFunc != null) { item = element.Item2; needAdd = onOnceDequeueProcessFunc (i, item); } } , (x, y, z) => //catch { qpcc .CaughtExceptionsPerformanceCounter .Increment(); z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z; if (onDequeueProcessCaughtExceptionProcessFunc != null) { reThrowException = onDequeueProcessCaughtExceptionProcessFunc ( x , y , z ); } else if (OnCaughtException != null) { reThrowException = OnCaughtException(this, x, y, z); } return(reThrowException); } , onDequeueProcessFinallyProcessAction //finally , null , qpcc.IncrementCountersAfterCountPerformanceForDequeue ); #endregion while queue.IsEmpty loop //池化 if (stopwatchEnqueue != null) { stopwatchEnqueue.Reset(); var r = _stopwatchsPool.TryPut(stopwatchEnqueue); if (!r) { stopwatchEnqueue.Stop(); stopwatchEnqueue = null; } } if (stopwatchDequeue != null) { stopwatchDequeue.Reset(); var r = _stopwatchsPool.TryPut(stopwatchDequeue); if (!r) { stopwatchDequeue.Stop(); stopwatchDequeue = null; } } if (onBatchDequeuesProcessAction != null) { //var item = element.Item2; if (needAdd) { i++; list.Add(item); } } } } else { if (sleepInMilliseconds > 0) { Thread.Sleep(sleepInMilliseconds); } } if (onBatchDequeuesProcessAction != null) { if ( i >= waitOneBatchMaxDequeuedTimes || stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds ) { if (i > 0) { if (stopwatch != null) { stopwatch.Stop(); } EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .TryCountPerformance ( PerformanceCounterProcessingFlagsType.All , _performanceCountersCategoryNameForBatchProcess , _performanceCountersCategoryInstanceNameForBatchProcess , () => { return(OnGetEnabledCountPerformanceProcessFunc()); } , () => { if (needBatchGroupBy) { var groups = list .GroupBy ( (x) => { return(keySelector(x)); } ); groups .AsParallel() .ForAll ( (group) => { onBatchDequeuesProcessAction ( i , needBatchGroupBy , group.Key , group.AsEnumerable() ); } ); //foreach (var group in groups) //{ //} } else { onBatchDequeuesProcessAction ( i , needBatchGroupBy , default(TBatchGroupByKey) , list ); } } , null , (xx, yy, zz) => { var rrr = false; zz = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz; if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null) { rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc ( xx, yy, zz ); } else if (OnCaughtException != null) { rrr = OnCaughtException(this, xx, yy, zz); } return(rrr); } , (xx, yy, zz, ww) => { i = 0; list.Clear(); if (stopwatch != null) { stopwatch.Restart(); } onDequeuesBatchProcessFinallyProcessAction? .Invoke(xx, yy, zz, ww); } ); } } } } , false , (x, y, z) => { var rr = false; if (OnCaughtException != null) { rr = OnCaughtException ( this, x, y, z ); } return(rr); } ); } }
public static void ChangeAverageTimerCounterValueWithTryCatchExceptionFinally ( this PerformanceCounter performanceCounter , bool enabled , PerformanceCounter basePerformanceCounter , Stopwatch stopwatch , Action onCountPerformanceInnerProcessAction = null , Func <PerformanceCounter, Exception, bool> onCaughtExceptionProcessFunc = null , Action <PerformanceCounter, PerformanceCounter, bool, Exception> onFinallyProcessAction = null ) { //Stopwatch stopwatch = null; if (enabled) { stopwatch.Reset(); stopwatch.Start(); } if (onCountPerformanceInnerProcessAction != null) { bool reThrowException = false; TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { onCountPerformanceInnerProcessAction(); } , reThrowException , (x, y) => { var r = reThrowException; if (onCaughtExceptionProcessFunc != null) { r = onCaughtExceptionProcessFunc(performanceCounter, x); } return(r); } , (x, y) => { if (enabled && stopwatch != null && stopwatch.IsRunning) { stopwatch.Stop(); performanceCounter.IncrementBy(stopwatch.ElapsedTicks); //stopwatch = null; basePerformanceCounter.Increment(); } if (onFinallyProcessAction != null) { onFinallyProcessAction ( performanceCounter , basePerformanceCounter , x , y ); } } ); } }
public static void TryCountPerformance ( PerformanceCounterProcessingFlagsType enabledProcessingFlagsType , string categoryName , string instanceName , Func <bool> onGetEnableCountProcessFunc = null , Action onCountPerformanceInnerProcessAction = null , Func <PerformanceCounterProcessingFlagsType, PerformanceCounter, long> onPerformanceCounterChangeValueProcessFunc = null , Func <Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null , Action <bool, Exception, Exception, string> onFinallyProcessAction = null , PerformanceCounterInstanceLifetime instanceLifetime = PerformanceCounterInstanceLifetime.Global , long?initializeInstanceRawValue = null ) { var enabledCountPerformance = true; Stopwatch[] stopwatches = null; var container = CountPerformanceBegin ( enabledProcessingFlagsType , categoryName , instanceName , out enabledCountPerformance , out stopwatches , onGetEnableCountProcessFunc , onPerformanceCounterChangeValueProcessFunc , instanceLifetime , initializeInstanceRawValue ); var reThrowException = false; #region Count Inner TryCatchFinally TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { onCountPerformanceInnerProcessAction(); } , reThrowException , (x, y, z) => { container .CaughtExceptionsPerformanceCounter .Increment(); var r = reThrowException; if (onCaughtExceptionProcessFunc != null) { r = onCaughtExceptionProcessFunc ( x , y , z ); } return(r); } , (x, y, z, w) => { if (onFinallyProcessAction != null) { onFinallyProcessAction ( x , y , z , w ); } } ); #endregion if (enabledCountPerformance) { CountPerformanceEnd ( enabledProcessingFlagsType , categoryName , instanceName , stopwatches , onPerformanceCounterChangeValueProcessFunc ); } }
public static void TryCountPerformance ( Func <bool> onGetEnableCountProcessFunc = null , bool reThrowException = false , PerformanceCounter[] IncrementCountersBeforeCountPerformance = null , PerformanceCounter[] DecrementCountersBeforeCountPerformance = null , WriteableTuple < bool //before时是否已经启动 , Stopwatch , PerformanceCounter , PerformanceCounter //base计数器 >[] timerCounters = null , Action onTryCountPerformanceProcessAction = null , Func <Exception, Exception, string, bool> onCaughtExceptionCountPerformanceProcessFunc = null , Action <bool, Exception, Exception, string> onFinallyCountPerformanceProcessAction = null , PerformanceCounter[] DecrementCountersAfterCountPerformance = null , PerformanceCounter[] IncrementCountersAfterCountPerformance = null ) { var enabledCountPerformance = true; { if (onGetEnableCountProcessFunc != null) { enabledCountPerformance = onGetEnableCountProcessFunc(); } } if ( enabledCountPerformance && onTryCountPerformanceProcessAction != null ) { if (enabledCountPerformance) { #region before if (IncrementCountersBeforeCountPerformance != null) { Array .ForEach ( IncrementCountersBeforeCountPerformance , (x) => { var l = x.Increment(); } ); } if (DecrementCountersBeforeCountPerformance != null) { Array .ForEach ( DecrementCountersBeforeCountPerformance , (x) => { var l = x.Decrement(); if (l < 0) { x.RawValue = 0; } } ); } if (timerCounters != null) { Array .ForEach ( timerCounters , (x) => { if ( x.Item1 && x.Item2 != null ) { x.Item2.Restart(); } } ); } #endregion } var needTry = true; TryCatchFinallyProcessHelper .TryProcessCatchFinally ( needTry , () => { onTryCountPerformanceProcessAction(); } , reThrowException , (x, y, z) => { if (onCaughtExceptionCountPerformanceProcessFunc != null) { reThrowException = onCaughtExceptionCountPerformanceProcessFunc ( x , y , z ); } return(reThrowException); } , (x, y, z, w) => { if (enabledCountPerformance) { #region after if (timerCounters != null) { Array .ForEach ( timerCounters , (xx) => { if (xx.Item2 != null) { var stopwatch = xx.Item2; stopwatch.Stop(); long elapsedTicks = stopwatch.ElapsedTicks; var counter = xx.Item3; counter.IncrementBy(elapsedTicks); //池化 //stopwatch = null; counter = xx.Item4; //base counter.Increment(); } } ); } if (IncrementCountersAfterCountPerformance != null) { Array .ForEach ( IncrementCountersAfterCountPerformance , (xx) => { var l = xx.Increment(); } ); } if (DecrementCountersAfterCountPerformance != null) { Array .ForEach ( DecrementCountersAfterCountPerformance , (xx) => { var l = xx.Decrement(); if (l < 0) { xx.RawValue = 0; } } ); } #endregion } if (onFinallyCountPerformanceProcessAction != null) { onFinallyCountPerformanceProcessAction(x, y, z, w); } } ); } }
public bool SendDataToSyncWaitResponseWithRetry <TToken> ( byte[] data , IPEndPoint remoteIPEndPoint , bool blockForResponse , AutoResetEvent waiter , TToken token , Func < TToken , Tuple < bool //是否继续等待 , bool //送达结果(是否收到应答) > > onSetAutoResetEventProcessFunc , out int sendTimes , out long elapsedMilliseconds , Stopwatch stopWatch = null , Func <int, TToken, byte[]> onBeforeSendOnceProcessFunc = null , Action <int, byte[]> onAfterSendedOnceProcessAction = null , Func <SocketAsyncDataHandler <T>, Exception, bool> onCaughtExceptionProcessFunc = null , int sendMaxTimes = 100 , int resendWaitOneIntervalsInMillisecondsFactor = 1 , int waitOneIntervalsInMilliseconds = 1000 ) { var r = false; elapsedMilliseconds = 0; int tempSendTimes = 0; long tempElapsedMilliseconds = 0; if (stopWatch != null) { if (!stopWatch.IsRunning) { stopWatch.Start(); } } AutoResetEvent autoResetEvent = waiter; int i = 0; var nextWaitOneIntervalsInMilliseconds = waitOneIntervalsInMilliseconds; TryCatchFinallyProcessHelper .TryProcessCatchFinally ( true , () => { var continueWaiting = true; while ( i < sendMaxTimes && continueWaiting && !r ) { #region loop body if (onBeforeSendOnceProcessFunc != null) { data = onBeforeSendOnceProcessFunc(i, token); } SendDataToSync ( data , remoteIPEndPoint ); i++; if (onAfterSendedOnceProcessAction != null) { onAfterSendedOnceProcessAction(i, data); } if (!blockForResponse) { r = true; break; } Tuple <bool, bool> tuple = null; if (autoResetEvent != null) { tuple = onSetAutoResetEventProcessFunc(token); if (tuple.Item1) //是否继续等待 { //继续等待 bool b = autoResetEvent.WaitOne(nextWaitOneIntervalsInMilliseconds, true); if (b) { // 有信号 r = true; break; } nextWaitOneIntervalsInMilliseconds += (waitOneIntervalsInMilliseconds * resendWaitOneIntervalsInMillisecondsFactor); } else { //不继续等待 continueWaiting = false; if (!r) { r = tuple.Item2; //可能的送达结果=false 或未知 因为不继续等待了 } break; } } #endregion } } , false , (x, y) => //catch { var reThrowException = false; if (onCaughtExceptionProcessFunc != null) { reThrowException = onCaughtExceptionProcessFunc(this, x); } return(reThrowException); } , (x, y) => //finally { if (blockForResponse) { if (stopWatch != null) { if (stopWatch.IsRunning) { stopWatch.Stop(); } tempElapsedMilliseconds = stopWatch.ElapsedMilliseconds; } if (autoResetEvent != null) { if ( !autoResetEvent.SafeWaitHandle.IsClosed && !autoResetEvent.SafeWaitHandle.IsInvalid ) { autoResetEvent.Close(); #if NET45 autoResetEvent.Dispose(); #endif autoResetEvent = null; //autoResetEventWaiter = null; } } } tempSendTimes = i; } ); sendTimes = tempSendTimes; elapsedMilliseconds = tempElapsedMilliseconds; return(r); }