From 7d7a04033597364708118443baa3344c184da1d8 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Mon, 24 Oct 2016 11:59:28 +0100 Subject: [PATCH] [CA-211267] Add a null check, use Run() to stop child classes directly calling InitialiseTicker() TickUntilCompletion() is now a static method and again takes an ApplianceAction parameter. It does a null check on each while iteration to handle the case where another thread nulls the action. Also added Run() method to ApplianceAction which initialises the ticker (now a private method), and let child classes call Run() instead. --- XenAdmin/Actions/OVFActions/ApplianceAction.cs | 16 +++++++++++----- .../Actions/OVFActions/ExportApplianceAction.cs | 5 ++--- .../Actions/OVFActions/ImportApplianceAction.cs | 4 +--- XenAdmin/Actions/OVFActions/ImportImageAction.cs | 5 ++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/XenAdmin/Actions/OVFActions/ApplianceAction.cs b/XenAdmin/Actions/OVFActions/ApplianceAction.cs index 3b4172822..23775355e 100644 --- a/XenAdmin/Actions/OVFActions/ApplianceAction.cs +++ b/XenAdmin/Actions/OVFActions/ApplianceAction.cs @@ -82,6 +82,12 @@ namespace XenAdmin.Actions.OVFActions Host = Helpers.GetMaster(connection); } + protected override void Run() + { + SafeToExit = false; + InitialiseTicker(); + } + protected void UpdateHandler(XenOvfTranportEventArgs e) { if (!string.IsNullOrEmpty(e.Message)) @@ -101,16 +107,16 @@ namespace XenAdmin.Actions.OVFActions m_transportAction.Cancel = true; } - protected void InitialiseTicker() + private void InitialiseTicker() { - System.Threading.Tasks.Task.Run(() => TickUntilCompletion()); + System.Threading.Tasks.Task.Run(() => TickUntilCompletion(this)); } - private void TickUntilCompletion() + private static void TickUntilCompletion(ApplianceAction action) { - while (!IsCompleted) + while (action != null && !action.IsCompleted) { - OnChanged(); + action.OnChanged(); Thread.Sleep(SLEEP_TIME); } } diff --git a/XenAdmin/Actions/OVFActions/ExportApplianceAction.cs b/XenAdmin/Actions/OVFActions/ExportApplianceAction.cs index cfcea029f..6073d64e4 100644 --- a/XenAdmin/Actions/OVFActions/ExportApplianceAction.cs +++ b/XenAdmin/Actions/OVFActions/ExportApplianceAction.cs @@ -90,11 +90,10 @@ namespace XenAdmin.Actions.OVFActions protected override void Run() { - SafeToExit = false; + base.Run(); + var session = Connection.Session; - InitialiseTicker(); - var url = session.Url; Uri uri = new Uri(url); diff --git a/XenAdmin/Actions/OVFActions/ImportApplianceAction.cs b/XenAdmin/Actions/OVFActions/ImportApplianceAction.cs index 350adfacd..52e52a99f 100644 --- a/XenAdmin/Actions/OVFActions/ImportApplianceAction.cs +++ b/XenAdmin/Actions/OVFActions/ImportApplianceAction.cs @@ -76,9 +76,7 @@ namespace XenAdmin.Actions.OVFActions protected override void Run() { - SafeToExit = false; - - InitialiseTicker(); + base.Run(); if (m_verifySignature) { diff --git a/XenAdmin/Actions/OVFActions/ImportImageAction.cs b/XenAdmin/Actions/OVFActions/ImportImageAction.cs index d270d085c..ce7c11582 100644 --- a/XenAdmin/Actions/OVFActions/ImportImageAction.cs +++ b/XenAdmin/Actions/OVFActions/ImportImageAction.cs @@ -70,10 +70,9 @@ namespace XenAdmin.Actions.OVFActions protected override void Run() { - SafeToExit = false; - Debug.Assert(m_vmMappings.Count == 1, "There is one VM mapping"); + base.Run(); - InitialiseTicker(); + Debug.Assert(m_vmMappings.Count == 1, "There is one VM mapping"); string systemid = m_vmMappings.Keys.ElementAt(0); var mapping = m_vmMappings.Values.ElementAt(0);