Permalink
Browse files

loops should break on exit

  • Loading branch information...
1 parent dddbbef commit f84026f4e442bfdfe86472df93499969390b7e44 @lformella committed Nov 15, 2012
@@ -41,7 +41,9 @@ public class BackendPlugin : ABackendPlugin
static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
readonly BinaryFormatter _formatter = new BinaryFormatter();
+
bool _isSaveFile;
+
readonly object _saveObjectsLock = new object();
readonly object _saveFilesLock = new object();
readonly object _saveSearchesLock = new object();
@@ -54,6 +56,8 @@ public class BackendPlugin : ABackendPlugin
const int BackupDataTime = 900000;
+ bool _allowRunning = true;
+
#endregion
#region ABackendPlugin
@@ -121,37 +125,43 @@ protected override void StartRun()
DateTime timeIrc = DateTime.Now;
DateTime timeStats = DateTime.Now;
- while (true)
+ DateTime _last = DateTime.Now;
+ while (_allowRunning)
{
- // Objects
- if ((DateTime.Now - timeIrc).TotalSeconds > BackupDataTime)
+ if (_last.AddSeconds(Settings.Instance.RunLoopTime) < DateTime.Now)
{
- timeIrc = DateTime.Now;
+ _last = DateTime.Now;
- SaveObjects();
- }
+ // Objects
+ if ((DateTime.Now - timeIrc).TotalSeconds > BackupDataTime)
+ {
+ timeIrc = DateTime.Now;
- // Files
- if (_isSaveFile)
- {
- SaveFiles();
- }
+ SaveObjects();
+ }
- // Statistics
- if ((DateTime.Now - timeStats).TotalSeconds > Settings.Instance.BackupStatisticTime)
- {
- timeStats = DateTime.Now;
- Statistic.Instance.Save();
- }
+ // Files
+ if (_isSaveFile)
+ {
+ SaveFiles();
+ }
- Thread.Sleep(Settings.Instance.RunLoopTime * 1000);
+ // Statistics
+ if ((DateTime.Now - timeStats).TotalSeconds > Settings.Instance.BackupStatisticTime)
+ {
+ timeStats = DateTime.Now;
+ Statistic.Instance.Save();
+ }
+ }
- // TODO break this!
+ Thread.Sleep(500);
}
}
protected override void StopRun()
{
+ _allowRunning = false;
+
// sync all to disk
SaveFiles();
SaveObjects();
@@ -40,6 +40,8 @@ public class Plugin : APlugin
readonly string _salt = BitConverter.ToString(new SHA256Managed().ComputeHash(BitConverter.GetBytes(new Random().Next()))).Replace("-", "");
+ bool _allowRunning = true;
+
#endregion
#region AWorker
@@ -69,7 +71,7 @@ protected override void StartRun()
var fileLoader = new FileLoader {Salt = _salt};
- while (true)
+ while (_allowRunning)
{
#if !UNSAFE
try
@@ -106,6 +108,8 @@ protected override void StartRun()
protected override void StopRun()
{
+ _allowRunning = false;
+
_listener.Close();
}
@@ -46,6 +46,8 @@ public class Connection : AConnection
SocketErrorCode _errorCode = 0;
+ bool _allowRunning;
+
#endregion
#region CONNECT / RECEIVE DATA
@@ -89,6 +91,7 @@ public override void Connect()
FireConnected();
+ _allowRunning = true;
try
{
#region BINARY READING
@@ -148,7 +151,7 @@ public override void Connect()
_log.Warn("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") no data received");
break;
}
- } while (missing > 0);
+ } while (_allowRunning && missing > 0);
}
}
@@ -217,7 +220,7 @@ public override void Connect()
}
data = "";
}
- } while (true);
+ } while (_allowRunning);
}
}
@@ -247,6 +250,8 @@ public override void Connect()
public override void Disconnect()
{
+ _allowRunning = false;
+
if (_writer != null)
{
_writer.Close();
@@ -38,8 +38,6 @@ public class Parser : AParser
{
#region VARIABLES
- public Servers Parent { get; set; }
-
readonly IntValue _intValue;
readonly PrivateMessage _privateMessage;
readonly Notice _notice;
View
@@ -299,6 +299,8 @@ protected override void StartRun()
protected override void StopRun()
{
+ _servers.AllowRunning = false;
+
foreach (Core.Server serv in Servers.All)
{
_servers.ServerDisconnect(serv);
View
@@ -61,14 +61,12 @@ public Parser IrcParser
{
if (_ircParser != null)
{
- _ircParser.Parent = null;
_ircParser.AddDownload -= BotConnect;
_ircParser.RemoveDownload -= BotDisconnect;
}
_ircParser = value;
if (_ircParser != null)
{
- _ircParser.Parent = this;
_ircParser.AddDownload += BotConnect;
_ircParser.RemoveDownload += BotDisconnect;
}
@@ -80,12 +78,16 @@ public Parser IrcParser
readonly Dictionary<Core.Server, ServerConnection> _servers;
readonly Dictionary<Packet, BotConnection> _downloads;
+ public bool AllowRunning { set; get; }
+
#endregion
#region INIT
public Servers()
{
+ AllowRunning = true;
+
_servers = new Dictionary<Core.Server, ServerConnection>();
_downloads = new Dictionary<Packet, BotConnection>();
@@ -330,21 +332,23 @@ void BotDisconnected(Packet aPacket, BotConnection aCon)
void RunTimer()
{
- while (true)
+ DateTime _last = DateTime.Now;
+ while (AllowRunning)
{
- foreach (var kvp in _servers)
+ if (_last.AddSeconds(Settings.Instance.RunLoopTime) < DateTime.Now)
{
- ServerConnection sc = kvp.Value;
- if (sc.IsRunning)
+ foreach (var kvp in _servers)
{
- sc.TriggerTimerRun();
+ ServerConnection sc = kvp.Value;
+ if (sc.IsRunning)
+ {
+ sc.TriggerTimerRun();
+ }
}
}
- Thread.Sleep(Settings.Instance.RunLoopTime * 1000);
+ Thread.Sleep(500);
}
-
- // TODO break this!
}
#endregion
@@ -67,7 +67,7 @@ protected override void StartRun()
}
}
- Thread.Sleep(1000);
+ Thread.Sleep(500);
}
}

0 comments on commit f84026f

Please sign in to comment.