private void btn_CleanBots_Click(object sender, EventArgs e) { for (int i = list_Bots.Items.Count - 1; i >= 0; --i) { try { BotListViewItem item = list_Bots.Items[i] as BotListViewItem; if (item.Client.IsGameDisconnected) { list_Bots.Items.RemoveAt(i); try { item.Runner.Dispose(); } catch { } } } catch (Exception err) { Console.WriteLine(err.Message); } } }
//------------------------------------------------------------------------------------------------------------- #region OP private void StartBots(string name_prefix = null, int count = 1, bool keep = false) { var add = AddBotConfig.TryLoadAddConfig(); if (name_prefix != null) { add.name_format = name_prefix; add.count = count; add.index = lastIndex; } else { var servers = RPGClientTemplateManager.Instance.GetAllServers(); var servers_optionals = new DeepEditor.Common.G2D.DataGrid.OptionalList(); servers_optionals.AddRange(servers); servers_optionals.Converter = new Func <System.Reflection.MemberInfo, object, object>((field, value) => { if (value is Data.ServerInfo server) { return(server.id); } return(value); }); add.index = lastIndex; if (keep == false) { var dialog = new G2DPropertyDialog <AddBotConfig>(add); { dialog.Text = "1"; dialog.PropertyGrid.SelectedDescriptorObject.AppendOptionals(nameof(AddBotConfig.serverID), servers_optionals); var res = dialog.ShowDialog(); if (res == System.Windows.Forms.DialogResult.OK) { add = dialog.SelectedObject; } else { return; } } } else { add.count = count; } // add = G2DPropertyDialog<AddBotConfig>.Show("1", add); } if (add != null) { try { if (add.name_format.Contains("{0}") && !string.IsNullOrEmpty(add.digit_format)) { var starting = new List <BotListViewItem>(); for (int i = 0; i < add.count; i++) { var id = add.index + i; var name = string.Format(add.name_format, id.ToString(add.digit_format)); var item = new BotListViewItem(name, id, add, config); list_Bots.Items.Add(item); starting.Add(item); } Task.Run(async() => { foreach (var e in starting) { if (config.AddBotIntervalMS > 0) { await Task.Delay(config.AddBotIntervalMS); } this.Invoke(new Action(() => { e.Runner.Start(); })); } }).ConfigureAwait(true); } else { var name = add.name_format; var item = new BotListViewItem(name, lastIndex, add, config); list_Bots.Items.Add(item); item.Runner.Start(); } } catch (Exception err) { MessageBox.Show(err.Message); } this.started = true; this.lastIndex = add.index + add.count; if (keep == false) { AddBotConfig.TrySaveAddConfig(add); } } }