📄 retryaction.cs
字号:
namespace Imps.Client.Core.Login
{
using Imps.Client.Core;
using Imps.Utils;
using System;
using System.Collections.Generic;
public sealed class RetryAction
{
private bool _executing;
private bool _isStarted;
private readonly ILoginListner _listner;
private readonly EventHandler _onTimer;
private readonly List<RetryActivity> _retryActivities;
private readonly object _syncRoot = new object();
private const int RetryInterval = 0xbb8;
public RetryAction(ILoginListner listner, List<LoginAction> actions)
{
this._listner = listner;
this._onTimer = new EventHandler(this.OnTimer);
this._retryActivities = new List<RetryActivity>();
this.InitRetryActivities(actions);
}
private void InitRetryActivities(List<LoginAction> actions)
{
try
{
List<LoginAction>.Enumerator enumerator = actions.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
LoginAction action = enumerator.get_Current();
this._retryActivities.Add(new RetryActivity(action));
}
}
finally
{
enumerator.Dispose();
}
}
catch (Exception exception)
{
this._listner.NotifyException(LoginStage.Retry, "InitRetryActivities failed.", exception);
}
}
private void OnTimer(object sender, EventArgs e)
{
if (!this._executing)
{
lock (this._syncRoot)
{
if (this._executing)
{
return;
}
this._executing = true;
}
try
{
for (int i = this._retryActivities.get_Count() - 1; i >= 0; i--)
{
try
{
RetryActivity activity = this._retryActivities.get_Item(i);
string message = string.Format("开始对{0}进行第{1}次重试。", activity._action.LoginStage, activity.FailedTimes + 1);
this._listner.NotifyMessage(LoginStage.Retry, message);
if (!activity.Execute())
{
this._retryActivities.RemoveAt(i);
message = string.Format("对{0}进行的第{1}次重试执行成功。", activity._action.LoginStage, activity.FailedTimes + 1);
this._listner.NotifyMessage(LoginStage.Retry, message);
}
else
{
message = string.Format("对{0}进行的第{1}次重试执行失败。", activity._action.LoginStage, activity.FailedTimes + 1);
this._listner.NotifyMessage(LoginStage.Retry, message);
}
}
catch (Exception exception)
{
this._listner.NotifyException(LoginStage.Retry, "OnTimer method failed.", exception);
}
}
if (this._retryActivities.get_Count() == 0)
{
this.Stop();
}
}
finally
{
this._executing = false;
}
}
}
public void Run()
{
try
{
if (!this._isStarted)
{
GlobalTimer.Register(this._onTimer, 0xbb8);
this._isStarted = true;
this._listner.NotifyMessage(LoginStage.Retry, "Register global timer successful.");
}
}
catch (Exception exception)
{
this._listner.NotifyException(LoginStage.Retry, "Register global timer failed.", exception);
}
}
private void Stop()
{
try
{
GlobalTimer.Unregister(this._onTimer);
this._listner.NotifyMessage(LoginStage.Retry, "Unregister global timer successful.");
}
catch (Exception exception)
{
this._listner.NotifyException(LoginStage.Retry, "Unregister global timer failed.", exception);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -