Put pCampbot "disconnect" command on separate thread like "connect" so that we can continue to run status commands whilst bots are disconnecting.

0.8-extended
Justin Clark-Casey (justincc) 2014-08-05 01:15:07 +01:00 committed by Justin Clark-Casey
parent 9fe282f902
commit 04cb9bc4bf
1 changed files with 44 additions and 36 deletions

View File

@ -611,46 +611,54 @@ namespace pCampBot
private void HandleDisconnect(string module, string[] cmd)
{
List<Bot> connectedBots;
int botsToDisconnectCount;
lock (m_bots)
{
int botsToDisconnect;
int connectedBots = m_bots.Count(b => b.ConnectionState == ConnectionState.Connected);
connectedBots = m_bots.FindAll(b => b.ConnectionState == ConnectionState.Connected);
if (cmd.Length == 1)
{
botsToDisconnect = connectedBots;
botsToDisconnectCount = connectedBots.Count;
}
else
{
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[1], out botsToDisconnect))
if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[1], out botsToDisconnectCount))
return;
botsToDisconnect = Math.Min(botsToDisconnect, connectedBots);
botsToDisconnectCount = Math.Min(botsToDisconnectCount, connectedBots.Count);
}
Thread disconnectBotThread = new Thread(o => DisconnectBotsInternal(connectedBots, botsToDisconnectCount));
disconnectBotThread.Name = "Bots disconnection thread";
disconnectBotThread.Start();
}
private void DisconnectBotsInternal(List<Bot> connectedBots, int disconnectCount)
{
DisconnectingBots = true;
MainConsole.Instance.OutputFormat("Disconnecting {0} bots", botsToDisconnect);
MainConsole.Instance.OutputFormat("Disconnecting {0} bots", disconnectCount);
int disconnectedBots = 0;
for (int i = m_bots.Count - 1; i >= 0; i--)
for (int i = connectedBots.Count - 1; i >= 0; i--)
{
if (disconnectedBots >= botsToDisconnect)
if (disconnectedBots >= disconnectCount)
break;
Bot thisBot = m_bots[i];
Bot thisBot = connectedBots[i];
if (thisBot.ConnectionState == ConnectionState.Connected)
{
Util.FireAndForget(o => thisBot.Disconnect());
thisBot.Disconnect();
disconnectedBots++;
}
}
DisconnectingBots = false;
}
}
private void HandleSit(string module, string[] cmd)
{