private bool ValidateTexts(bool flushCache = false) { // Clean! vms = null; if (flushCache) { _hn_cache = null; } // A clean alternative way to run consecutive network events BackgroundWorkerQueueDialog backgroundWorker = new BackgroundWorkerQueueDialog("Retrieving Network Information"); backgroundWorker.AppendTask("Starting...", NetworkWorkers.GetStarterWorker(new NetworkWorkerObject { HostName = hostnameText.Text, IpAddress = ipText.Text, MacAddress = macText.Text })); backgroundWorker.AppendTask("Getting IP address from server...", NetworkWorkers.GetIpWorker()); backgroundWorker.AppendTask("Getting MAC address from IP...", NetworkWorkers.GetMacWorker()); backgroundWorker.ShowDialog(); var v = backgroundWorker.GetWorker(); // Set fields, only if the returned object has text - therefore preserving whatever was typed beforehand if (backgroundWorker.GetWorker().ReturnedObjects[1].s == StatusCode.OK) { ipText.Text = ((NetworkWorkerObject)backgroundWorker.GetWorker().ReturnedObjects[1].o).IpAddress; } if (backgroundWorker.GetWorker().ReturnedObjects[2].s == StatusCode.OK) { macText.Text = ((NetworkWorkerObject)backgroundWorker.GetWorker().ReturnedObjects[2].o).MacAddress; } // If something's empty/failed, let user know hostnameText.IsValid = (string.IsNullOrWhiteSpace(hostnameText.Text)) ? tribool.FALSE : tribool.TRUE; ipText.IsValid = (string.IsNullOrWhiteSpace(ipText.Text) || backgroundWorker.GetWorker().ReturnedObjects[1].s != StatusCode.OK) ? tribool.FALSE : tribool.TRUE; macText.IsValid = (string.IsNullOrWhiteSpace(macText.Text) || backgroundWorker.GetWorker().ReturnedObjects[2].s != StatusCode.OK) ? tribool.FALSE : tribool.TRUE; // Check if the PowerShell thing is on... backgroundWorker = new BackgroundWorkerQueueDialog("Checking for PSRemoting and WinRM...", ProgressBarStyle.Marquee); backgroundWorker.AppendTask("Pinging...", DummyWorker.GetWorker((ctx) => { try { Interface.BringOnline(hostnameText.Text); } catch (Exception e) { MessageBox.Show(e.Message, "Exception", MessageBoxButtons.OK); ctx.s = StatusCode.FAILED; return(ctx); } ctx.s = StatusCode.OK; return(ctx); })); backgroundWorker.ShowDialog(); if (backgroundWorker.GetWorker().ReturnedObjects[0].s != StatusCode.OK) { return(false); } // Get VMs // Check if the vms in the cache are different if (_hn_cache == hostnameText.Text) { return(true); } listBox1.Items.Clear(); backgroundWorker = new BackgroundWorkerQueueDialog("Scanning for Virtual Machines...", ProgressBarStyle.Marquee); backgroundWorker.AppendTask("Getting machines...", DummyWorker.GetWorker(() => { try { _hn_cache = hostnameText.Text; vms = HyperV.GetVm(hostnameText.Text); vms?.ForEach(x => { ThreadManager.Invoke(this, listBox1, () => listBox1.Items.Add(x.Name + " [" + x.Uuid.ToString().ToUpper() + "]")); }); } catch (Exception e) { MessageBox.Show(e.Message, "Exception", MessageBoxButtons.OK); } })); backgroundWorker.ShowDialog(); return(true); }
private void AddTemplateDialog_Load(object sender, EventArgs e) { // Get server information BackgroundWorkerQueueDialog backgroundWorker = new BackgroundWorkerQueueDialog("Retrieving Network Information"); foreach (var host in SessionManager.GetDatabase().GetServerDb()) { backgroundWorker.AppendTask("Validating " + host.HostName, NetworkWorkers.GetStarterWorker( new NetworkWorkerObject { HostName = host.HostName } )); backgroundWorker.AppendTask("Getting IP address of " + host.HostName + "...", NetworkWorkers.GetIpWorker()); } backgroundWorker.ShowDialog(); var bw = backgroundWorker.GetWorker(); for (int i = 0; i < SessionManager.GetDatabase().GetServerDb().Count; i++) { if (bw.ReturnedObjects[i * 2 + 1].s == StatusCode.OK) { hostComputers.Add(new HostComputer { HostName = ((NetworkWorkerObject)bw.ReturnedObjects[i * 2 + 1].o).HostName, IpAddress = ((NetworkWorkerObject)bw.ReturnedObjects[i * 2 + 1].o).IpAddress, }); } } // Get configurations for (int i = 0; i < jsonHelper.JObject["templates"]?.Count(); i++) { configComboBox.ComboBox.Items.Add(jsonHelper.JObject["templates"][i]["name"]); } // Add options to host combobox hostComputers.ForEach(x => { if (!string.IsNullOrEmpty(x.HostName) && !string.IsNullOrEmpty(x.IpAddress)) { serverComboBox.ComboBox.Items.Add(x.HostName + " [" + x.IpAddress + "]"); } }); // Get virtual machines backgroundWorker = new BackgroundWorkerQueueDialog("Scanning for Virtual Machines...", ProgressBarStyle.Marquee); backgroundWorker.AppendTask("Getting machines...", DummyWorker.GetWorker((ctx) => { SessionManager.GetDatabase().FlushCache(PsStreamEventHandlers.GetUIHandlers(ctx)); try { foreach (var vm in SessionManager.GetDatabase().GetVm(VirtualMachineType.BASE)) { virtualMachines.Add(vm); } virtualMachines.ForEach(x => { ThreadManager.Invoke(this, baseComboBox, () => baseComboBox.ComboBox.Items.Add(x.Name + " [" + x.Host + "]") ); }); ctx.s = StatusCode.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK); ctx.s = StatusCode.FAILED; } return(ctx); })); backgroundWorker.ShowDialog(); }
private void BrowseVhdDialog_Load(object sender, EventArgs e) { // Get server information BackgroundWorkerQueueDialog backgroundWorker = new BackgroundWorkerQueueDialog("Retrieving Network Information"); foreach (var host in SessionManager.GetDatabase().GetServerDb()) { backgroundWorker.AppendTask("Validating " + host.HostName, NetworkWorkers.GetStarterWorker( new NetworkWorkerObject { HostName = host.HostName } )); backgroundWorker.AppendTask("Getting IP address of " + host.HostName + "...", NetworkWorkers.GetIpWorker()); } backgroundWorker.ShowDialog(); var bw = backgroundWorker.GetWorker(); for (int i = 0; i < SessionManager.GetDatabase().GetServerDb().Count; i++) { if (bw.ReturnedObjects[i * 2 + 1].s == StatusCode.OK) { hostComputers.Add(new HostComputer { HostName = ((NetworkWorkerObject)bw.ReturnedObjects[i * 2 + 1].o).HostName, IpAddress = ((NetworkWorkerObject)bw.ReturnedObjects[i * 2 + 1].o).IpAddress, }); } } // Add options to host combobox hostComputers.ForEach(x => { if (!string.IsNullOrEmpty(x.HostName) && !string.IsNullOrEmpty(x.IpAddress)) { serverComboBox.ComboBox.Items.Add(x.HostName + " [" + x.IpAddress + "]"); } }); serverComboBox.ComboBox.SelectedIndexChanged += (o, ev) => { if (Dns.GetHostName() != GetSelectedHost().HostName) { button3.Enabled = false; } else { button3.Enabled = true; } }; }