private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Host affinityHost             = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);
            VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || !oldMigrateToHomeCmdCanRun && !cpmCmdHome.CanExecute())
                {
                    firstItem.Command = cmdHome;
                }
                else
                {
                    firstItem.Command = oldMigrateToHomeCmdCanRun ? cmdHome : cpmCmdHome;
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            // Adds the migrate wizard button, do this before the enable checks on the other items
            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (_isDropDownClosed)
                {
                    // Stop making requests to assert can start on each host after dropdown is closed
                    break;
                }

                Host host = item.Tag as Host;
                if (host != null)
                {
                    VMOperationCommand      cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name().EscapeAmpersands(), _operation, session);
                    CrossPoolMigrateCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host, _resumeAfter);

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (_operation == vm_operations.start_on || (!oldMigrateCmdCanRun && !cpmCmd.CanExecute() && string.IsNullOrEmpty(cpmCmd.CantExecuteReason)))
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Host affinityHost             = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);
            VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || !oldMigrateToHomeCmdCanRun && !cpmCmdHome.CanExecute())
                {
                    firstItem.Command = cmdHome;
                }
                else
                {
                    firstItem.Command = oldMigrateToHomeCmdCanRun ? cmdHome : cpmCmdHome;
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (item.Tag is Host)
                {
                    Host host = (Host)item.Tag;

                    string hostNameText = host.Name.EscapeAmpersands();

                    VMOperationCommand cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, hostNameText, _operation, session);
                    VMOperationCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host)
                    {
                        MenuText = hostNameText
                    };

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (_operation == vm_operations.start_on || !oldMigrateCmdCanRun && !cpmCmd.CanExecute())
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }

            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;
            VMOperationCommand     commandForFirstMenuItem = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);

            Program.Invoke(Program.MainWindow, delegate
            {
                VMOperationToolStripMenuSubItem firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];
                firstItem.Command = commandForFirstMenuItem;
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (item.Tag is Host)
                {
                    Host host = (Host)item.Tag;

                    VMOperationCommand cmd    = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name.EscapeAmpersands(), _operation, session);
                    VMOperationCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host)
                    {
                        MenuText = host.Name.EscapeAmpersands()
                    };

                    VMOperationToolStripMenuSubItem tempItem = item;
                    Program.Invoke(Program.MainWindow, delegate
                    {
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if (!oldMigrateCmdCanRun && !cpmCmd.CanExecute())
                        {
                            tempItem.Command = cmd;
                        }
                        else
                        {
                            tempItem.Command = oldMigrateCmdCanRun ? cmd : cpmCmd;
                        }
                    });
                }
            }

            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));
        }
        private void EnableAppropriateHostsNoWlb(Session session)
        {
            SelectedItemCollection selection  = Command.GetSelection();
            IXenConnection         connection = selection[0].Connection;

            VMOperationCommand cmdHome = new VMOperationHomeServerCommand(Command.MainWindowCommandInterface, selection, _operation, session);
            Host affinityHost          = connection.Resolve(((VM)Command.GetSelection()[0].XenObject).affinity);

            Program.Invoke(Program.MainWindow, delegate
            {
                var firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];

                bool oldMigrateToHomeCmdCanRun = cmdHome.CanExecute();
                if (affinityHost == null || _operation == vm_operations.start_on || oldMigrateToHomeCmdCanRun)
                {
                    firstItem.Command = cmdHome;
                    firstItem.Enabled = oldMigrateToHomeCmdCanRun;
                }
                else
                {
                    VMOperationCommand cpmCmdHome = new CrossPoolMigrateToHomeCommand(Command.MainWindowCommandInterface, selection, affinityHost);

                    if (cpmCmdHome.CanExecute())
                    {
                        firstItem.Command = cpmCmdHome;
                        firstItem.Enabled = true;
                    }
                    else
                    {
                        firstItem.Command = cmdHome;
                        firstItem.Enabled = false;
                    }
                }
            });

            List <VMOperationToolStripMenuSubItem> dropDownItems = DropDownItems.Cast <VMOperationToolStripMenuSubItem>().ToList();

            // Adds the migrate wizard button, do this before the enable checks on the other items
            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));

            foreach (VMOperationToolStripMenuSubItem item in dropDownItems)
            {
                if (_isDropDownClosed)
                {
                    // Stop making requests to assert can start on each host after dropdown is closed
                    break;
                }

                Host host = item.Tag as Host;
                if (host != null)
                {
                    // API calls could happen in CanExecute(), which take time to wait.
                    // So a Producer-Consumer-Queue with size 25 is used here to :
                    //   1. Make API calls for different menu items happen in parallel;
                    //   2. Limit the count of concurrent threads (now it's 25).
                    workerQueueWithouWlb.EnqueueItem(() =>
                    {
                        if (_isDropDownClosed)
                        {
                            return;
                        }
                        VMOperationCommand cmd         = new VMOperationHostCommand(Command.MainWindowCommandInterface, selection, delegate { return(host); }, host.Name().EscapeAmpersands(), _operation, session);
                        CrossPoolMigrateCommand cpmCmd = new CrossPoolMigrateCommand(Command.MainWindowCommandInterface, selection, host, _resumeAfter);

                        VMOperationToolStripMenuSubItem tempItem = item;
                        bool oldMigrateCmdCanRun = cmd.CanExecute();
                        if ((_operation == vm_operations.start_on) || oldMigrateCmdCanRun)
                        {
                            Program.Invoke(Program.MainWindow, delegate
                            {
                                tempItem.Command = cmd;
                                tempItem.Enabled = oldMigrateCmdCanRun;
                            });
                        }
                        else
                        {
                            bool crossPoolMigrateCmdCanRun = cpmCmd.CanExecute();
                            if (crossPoolMigrateCmdCanRun || !string.IsNullOrEmpty(cpmCmd.CantExecuteReason))
                            {
                                Program.Invoke(Program.MainWindow, delegate
                                {
                                    tempItem.Command = cpmCmd;
                                    tempItem.Enabled = crossPoolMigrateCmdCanRun;
                                });
                            }
                            else
                            {
                                Program.Invoke(Program.MainWindow, delegate
                                {
                                    tempItem.Command = cmd;
                                    tempItem.Enabled = oldMigrateCmdCanRun;
                                });
                            }
                        }
                    });
                }
            }
        }