CA-202377: Removed last instance of the QueuedBackgroundWorker and deleted the class.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2018-08-08 13:54:48 +01:00 committed by Mihaela Stoica
parent 78ecef617d
commit f4d92da5ee
5 changed files with 105 additions and 161 deletions

View File

@ -42,7 +42,6 @@ using XenAdmin.Core;
using XenAdmin.Network;
using XenAdmin.Properties;
using XenAPI;
using XenCenterLib;
namespace XenAdmin.Wizards.HAWizard_Pages
@ -53,7 +52,7 @@ namespace XenAdmin.Wizards.HAWizard_Pages
private IXenConnection connection;
private readonly CollectionChangeEventHandler VM_CollectionChangedWithInvoke;
private readonly QueuedBackgroundWorker m_worker;
private readonly List<VM> _vmsQueuedForUpdate = new List<VM>();
/// <summary>
/// May not be set to null.
@ -160,8 +159,6 @@ namespace XenAdmin.Wizards.HAWizard_Pages
nudStartDelay.Maximum = long.MaxValue;
nudOrder.Maximum = long.MaxValue;
m_worker = new QueuedBackgroundWorker();
}
/// <summary>
@ -256,7 +253,7 @@ namespace XenAdmin.Wizards.HAWizard_Pages
dataGridViewVms.Rows.AddRange(newRows.ToArray());
var addedVms = from row in dataGridViewVms.Rows.Cast<VmWithSettingsRow>() select row.Vm;
UpdateVMsAgility(addedVms);
UpdateVMsAgility(addedVms.ToList());
}
finally
{
@ -267,17 +264,26 @@ namespace XenAdmin.Wizards.HAWizard_Pages
/// <summary>
/// Starts a new background thread that updates the displayed agility status for each VM.
/// </summary>
private void UpdateVMsAgility(IEnumerable<VM> vms)
private void UpdateVMsAgility(List<VM> vms)
{
Debug.Assert(connection != null, "Connection property must have been set to non-null before calling this function");
if (bgWorker.IsBusy)
{
foreach (var vm in vms)
{
if (!_vmsQueuedForUpdate.Contains(vm))
_vmsQueuedForUpdate.Add(vm);
}
return;
}
//worker starts on UI (main) thread
m_worker.RunWorkerAsync((sender, arg) => worker_DoWork(null, vms), worker_RunWorkerCompleted);
Debug.Assert(connection != null, "Connection is null; set it to non-null before calling this method.");
if (vms.Count > 0)
bgWorker.RunWorkerAsync(vms);
}
private object worker_DoWork(object sender, object arg)
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
var vms = arg as IEnumerable<VM>;
var vms = e.Argument as IEnumerable<VM>;
Debug.Assert(vms != null);
Session session = connection.DuplicateSession();
@ -296,10 +302,10 @@ namespace XenAdmin.Wizards.HAWizard_Pages
}
}
return results;
e.Result = results;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
@ -346,6 +352,13 @@ namespace XenAdmin.Wizards.HAWizard_Pages
row.FriendlyNonAgileReason = nonAgileReason;
}
updateButtons();
if (_vmsQueuedForUpdate.Count > 0)
{
var vms = new List<VM>(_vmsQueuedForUpdate);
_vmsQueuedForUpdate.Clear();
UpdateVMsAgility(vms);
}
}
private bool IsHaActivatedFirstTime(IEnumerable<VM> vms)
@ -517,12 +530,12 @@ namespace XenAdmin.Wizards.HAWizard_Pages
private void m_dropDownButtonRestartPriority_Click(object sender, EventArgs e)
{
var selectedRows = dataGridViewVms.SelectedRows.Cast<VmWithSettingsRow>();
var selectedRows = dataGridViewVms.SelectedRows.Cast<VmWithSettingsRow>().ToList();
foreach (ToolStripMenuItem item in contextMenuStrip.Items)
{
var itemRestartPriority = ((VM.HA_Restart_Priority)item.Tag);
item.Checked = selectedRows.Count() > 0 && selectedRows.All(s => s.RestartPriority == itemRestartPriority);
item.Checked = selectedRows.Count > 0 && selectedRows.All(s => s.RestartPriority == itemRestartPriority);
}
}
@ -530,7 +543,7 @@ namespace XenAdmin.Wizards.HAWizard_Pages
{
Program.AssertOnEventThread();
var selectedRows = dataGridViewVms.SelectedRows.Cast<VmWithSettingsRow>();
var selectedRows = dataGridViewVms.SelectedRows.Cast<VmWithSettingsRow>().ToList();
if (dataGridViewVms.SelectedRows.Count == 0)
{
@ -578,11 +591,11 @@ namespace XenAdmin.Wizards.HAWizard_Pages
// set the order and delay NUDs
nudOrder.Enabled = nudStartDelay.Enabled = true;
var orderDistList = (from row in selectedRows select row.StartOrder).Distinct();
nudOrder.Text = orderDistList.Count() == 1 ? orderDistList.ElementAt(0).ToString() : "";
var orderDistList = (from row in selectedRows select row.StartOrder).Distinct().ToList();
nudOrder.Text = orderDistList.Count == 1 ? orderDistList.ElementAt(0).ToString() : "";
var delayDistList = (from row in selectedRows select row.StartDelay).Distinct();
nudStartDelay.Text = delayDistList.Count() == 1 ? delayDistList.ElementAt(0).ToString() : "";
var delayDistList = (from row in selectedRows select row.StartDelay).Distinct().ToList();
nudStartDelay.Text = delayDistList.Count == 1 ? delayDistList.ElementAt(0).ToString() : "";
// check that all the VMs selected in the list are agile and make sure the protect button is disabled with the relevant reason
VmWithSettingsRow nonAgileRow = selectedRows.FirstOrDefault(r => !r.IsAgile);

View File

@ -39,6 +39,7 @@ namespace XenAdmin.Wizards.HAWizard_Pages
this.colDelay = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colAgile = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.button1 = new System.Windows.Forms.Button();
this.bgWorker = new System.ComponentModel.BackgroundWorker();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxStatus)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudOrder)).BeginInit();
@ -155,8 +156,8 @@ namespace XenAdmin.Wizards.HAWizard_Pages
resources.ApplyResources(this.dataGridViewVms, "dataGridViewVms");
this.dataGridViewVms.MultiSelect = true;
this.dataGridViewVms.Name = "dataGridViewVms";
this.dataGridViewVms.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewVms_KeyDown);
this.dataGridViewVms.SelectionChanged += new System.EventHandler(this.dataGridViewVms_SelectionChanged);
this.dataGridViewVms.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewVms_KeyDown);
//
// colImage
//
@ -200,6 +201,12 @@ namespace XenAdmin.Wizards.HAWizard_Pages
this.button1.Name = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// bgWorker
//
this.bgWorker.WorkerSupportsCancellation = true;
this.bgWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bgWorker_DoWork);
this.bgWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted);
//
// AssignPriorities
//
resources.ApplyResources(this, "$this");
@ -239,6 +246,7 @@ namespace XenAdmin.Wizards.HAWizard_Pages
private System.Windows.Forms.DataGridViewTextBoxColumn colStartOrder;
private System.Windows.Forms.DataGridViewTextBoxColumn colDelay;
private System.Windows.Forms.DataGridViewTextBoxColumn colAgile;
private System.ComponentModel.BackgroundWorker bgWorker;
}

View File

@ -504,57 +504,21 @@
<metadata name="colImage.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colImage.HeaderText" xml:space="preserve">
<value />
</data>
<data name="colImage.Width" type="System.Int32, mscorlib">
<value>5</value>
</data>
<metadata name="colVm.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colVm.HeaderText" xml:space="preserve">
<value>Virtual machine</value>
</data>
<data name="colVm.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colRestartPriority.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colRestartPriority.HeaderText" xml:space="preserve">
<value>Restart priority</value>
</data>
<data name="colRestartPriority.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colStartOrder.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colStartOrder.HeaderText" xml:space="preserve">
<value>Start order</value>
</data>
<data name="colStartOrder.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colDelay.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colDelay.HeaderText" xml:space="preserve">
<value>Delay interval</value>
</data>
<data name="colDelay.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colAgile.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colAgile.HeaderText" xml:space="preserve">
<value>Agile</value>
</data>
<data name="colAgile.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<data name="dataGridViewVms.Dock" type="System.Windows.Forms.DockStyle, System.Windows.Forms">
<value>Fill</value>
</data>
@ -615,6 +579,60 @@
<data name="tableLayoutPanel1.LayoutSettings" type="System.Windows.Forms.TableLayoutSettings, System.Windows.Forms">
<value>&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;TableLayoutSettings&gt;&lt;Controls&gt;&lt;Control Name="pictureBoxStatus" Row="0" RowSpan="1" Column="0" ColumnSpan="1" /&gt;&lt;Control Name="labelHaStatus" Row="0" RowSpan="1" Column="1" ColumnSpan="5" /&gt;&lt;Control Name="labelProtectionLevel" Row="2" RowSpan="1" Column="0" ColumnSpan="2" /&gt;&lt;Control Name="m_dropDownButtonRestartPriority" Row="2" RowSpan="1" Column="2" ColumnSpan="2" /&gt;&lt;Control Name="labelStartOrder" Row="3" RowSpan="1" Column="0" ColumnSpan="3" /&gt;&lt;Control Name="nudOrder" Row="3" RowSpan="1" Column="3" ColumnSpan="1" /&gt;&lt;Control Name="labelStartDelay" Row="4" RowSpan="1" Column="0" ColumnSpan="3" /&gt;&lt;Control Name="nudStartDelay" Row="4" RowSpan="1" Column="3" ColumnSpan="1" /&gt;&lt;Control Name="labelStartDelayUnits" Row="4" RowSpan="1" Column="4" ColumnSpan="1" /&gt;&lt;Control Name="linkLabelTellMeMore" Row="5" RowSpan="1" Column="0" ColumnSpan="5" /&gt;&lt;Control Name="haNtolIndicator" Row="2" RowSpan="4" Column="5" ColumnSpan="1" /&gt;&lt;Control Name="dataGridViewVms" Row="1" RowSpan="1" Column="0" ColumnSpan="6" /&gt;&lt;/Controls&gt;&lt;Columns Styles="AutoSize,0,AutoSize,0,AutoSize,0,AutoSize,0,AutoSize,0,Percent,100" /&gt;&lt;Rows Styles="AutoSize,0,Percent,100,AutoSize,0,AutoSize,0,AutoSize,0,AutoSize,0,Absolute,20" /&gt;&lt;/TableLayoutSettings&gt;</value>
</data>
<metadata name="colImage.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colImage.HeaderText" xml:space="preserve">
<value />
</data>
<data name="colImage.Width" type="System.Int32, mscorlib">
<value>5</value>
</data>
<metadata name="colVm.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colVm.HeaderText" xml:space="preserve">
<value>Virtual machine</value>
</data>
<data name="colVm.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colRestartPriority.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colRestartPriority.HeaderText" xml:space="preserve">
<value>Restart priority</value>
</data>
<data name="colRestartPriority.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colStartOrder.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colStartOrder.HeaderText" xml:space="preserve">
<value>Start order</value>
</data>
<data name="colStartOrder.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colDelay.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colDelay.HeaderText" xml:space="preserve">
<value>Delay interval</value>
</data>
<data name="colDelay.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<metadata name="colAgile.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="colAgile.HeaderText" xml:space="preserve">
<value>Agile</value>
</data>
<data name="colAgile.MinimumWidth" type="System.Int32, mscorlib">
<value>50</value>
</data>
<data name="button1.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
@ -636,6 +654,9 @@
<data name="&gt;&gt;button1.Type" xml:space="preserve">
<value>System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<metadata name="bgWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>166, 17</value>
</metadata>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -690,6 +711,12 @@
<data name="&gt;&gt;colAgile.Type" xml:space="preserve">
<value>System.Windows.Forms.DataGridViewTextBoxColumn, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;bgWorker.Name" xml:space="preserve">
<value>bgWorker</value>
</data>
<data name="&gt;&gt;bgWorker.Type" xml:space="preserve">
<value>System.ComponentModel.BackgroundWorker, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;$this.Name" xml:space="preserve">
<value>AssignPriorities</value>
</data>

View File

@ -1,101 +0,0 @@
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System.Collections.Generic;
using System.ComponentModel;
namespace XenCenterLib
{
/// <summary>
/// This is thread-safe
/// </summary>
public class QueuedBackgroundWorker:BackgroundWorker
{
private Queue<BackgroundWorker> Queue = new Queue<BackgroundWorker>();
private object _lock = new object();
public delegate object DoWorkQueuedEventHandler(object sender, object argument);
public void RunWorkerAsync(DoWorkQueuedEventHandler doWork, RunWorkerCompletedEventHandler workerCompleted)
{
BackgroundWorker bw = GetBackgroundWorker(doWork, workerCompleted);
Queue.Enqueue(bw);
lock (_lock)
{
if (Queue.Count == 1)
{
((BackgroundWorker)this.Queue.Peek()).RunWorkerAsync();
}
}
}
private BackgroundWorker GetBackgroundWorker(DoWorkQueuedEventHandler doWork, RunWorkerCompletedEventHandler workerCompleted)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.DoWork += (sender, args) =>
{
if (doWork != null)
{
args.Result=doWork(this, new DoWorkEventArgs(args.Argument));
}
};
bw.RunWorkerCompleted += (sender, args) =>
{
if (workerCompleted != null)
{
object result = args.Error == null ? args.Result : null;
workerCompleted(this,
new RunWorkerCompletedEventArgs(result, args.Error,
args.Cancelled));
}
Queue.Dequeue();
lock (_lock)
{
if (Queue.Count > 0)
{
((BackgroundWorker)this.Queue.Peek()).RunWorkerAsync();
}
}
};
return bw;
}
}
}

View File

@ -87,9 +87,6 @@
<Compile Include="PathValidator.cs" />
<Compile Include="Processes.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QueuedBackgroundWorker.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Set.cs" />
<Compile Include="StreamUtilities.cs" />
<Compile Include="StringUtility.cs" />