Deregister events and dispose of web clients when removing them from the list.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2018-08-01 16:58:22 +01:00 committed by Mihaela Stoica
parent 23975c5ee6
commit 518951d868
2 changed files with 33 additions and 21 deletions

View File

@ -151,7 +151,7 @@ namespace XenAdmin.Core
Program.AssertOnEventThread(); Program.AssertOnEventThread();
// clear the _webClients so that an existing multiple-url Navigation is cancelled. // clear the _webClients so that an existing multiple-url Navigation is cancelled.
_webClients.Clear(); ClearWebClients();
base.OnNavigating(e); base.OnNavigating(e);
} }
@ -167,11 +167,10 @@ namespace XenAdmin.Core
/// <summary> /// <summary>
/// Navigates to the first valid URI in the specified list. /// Navigates to the first valid URI in the specified list.
/// </summary> /// </summary>
public void Navigate(IEnumerable<Uri> uris) public void Navigate(List<Uri> uriList)
{ {
Program.AssertOnEventThread(); Program.AssertOnEventThread();
Util.ThrowIfEnumerableParameterNullOrEmpty(uris, "uris"); Util.ThrowIfEnumerableParameterNullOrEmpty(uriList, "uris");
List<Uri> uriList = new List<Uri>(uris);
if (uriList.Count == 1) if (uriList.Count == 1)
{ {
@ -179,19 +178,19 @@ namespace XenAdmin.Core
return; return;
} }
// test each url with a WebClient to see if it works. ClearWebClients();
_webClients.Clear();
var proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(null, false); var proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(null, false);
_webClients.AddRange(uriList.ConvertAll(u => new WebClient() {Proxy = proxy}));
// start all urls downloading in parallel. // test each url with a WebClient to see if it works and start downloading for all in parallel
for (int i = 0; i < _webClients.Count; i++) foreach (var uri in uriList)
{ {
_webClients[i].DownloadDataCompleted += webClient_DownloadDataCompleted; var webClient = new WebClient {Proxy = proxy};
webClient.DownloadDataCompleted += webClient_DownloadDataCompleted;
_webClients.Add(webClient);
try try
{ {
_webClients[i].DownloadDataAsync(uriList[i], uriList[i]); webClient.DownloadDataAsync(uri, uri);
} }
catch (WebException) catch (WebException)
{ {
@ -208,23 +207,38 @@ namespace XenAdmin.Core
{ {
Program.AssertOnEventThread(); Program.AssertOnEventThread();
WebClient webClient = (WebClient)sender; var webClient = sender as WebClient;
if (webClient == null)
return;
if (_webClients.Contains(webClient)) if (_webClients.Contains(webClient))
{ {
webClient.DownloadDataCompleted -= webClient_DownloadDataCompleted;
_webClients.Remove(webClient); _webClients.Remove(webClient);
webClient.Dispose();
if (e.Error == null || (e.Error != null && _webClients.Count == 0)) if (e.Error == null || (e.Error != null && _webClients.Count == 0))
{ {
// either one has finished successfully...or...they've all failed. // either one has finished successfully or they've all failed and been removed one by one.
// navigate the browser to this url and leave other requests (if any) to timeout. // navigate the browser to this url and leave other requests (if any) to timeout.
_webClients.Clear(); ClearWebClients();
Navigate((Uri)e.UserState); Navigate((Uri)e.UserState);
} }
} }
else
// if the sender is not contained in the _webClients, it's because the _webClients have been
// cleared due to either a valid url having been found or another Navigate having started;
// in either case take no action
}
private void ClearWebClients()
{
foreach (var webClient in _webClients)
{ {
// either a valid url has been found...or another Navigate has started: do nothing. webClient.DownloadDataCompleted -= webClient_DownloadDataCompleted;
webClient.Dispose();
} }
_webClients.Clear();
} }

View File

@ -31,12 +31,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using NUnit.Framework; using NUnit.Framework;
using XenAdmin.Core; using XenAdmin.Core;
using System.IO; using System.IO;
using System.Threading;
using System.Net;
namespace XenAdminTests.MiscTests namespace XenAdminTests.MiscTests
{ {
@ -108,7 +106,7 @@ namespace XenAdminTests.MiscTests
_wb.NavigateError += (s, e) => Assert.Fail("Navigation failed."); _wb.NavigateError += (s, e) => Assert.Fail("Navigation failed.");
MW(() => _wb.Navigate(new[] { uri, uri2 })); MW(() => _wb.Navigate(new List<Uri> {uri, uri2}));
MWWaitFor(() => navigating && navigated, "Navigation didn't take place."); MWWaitFor(() => navigating && navigated, "Navigation didn't take place.");
} }
@ -155,7 +153,7 @@ namespace XenAdminTests.MiscTests
navError = true; navError = true;
}; };
MW(() => _wb.Navigate(new[] { uri, uri2 })); MW(() => _wb.Navigate(new List<Uri> {uri, uri2}));
MWWaitFor(() => navigating && navigated && navError, "Navigation didn't take place."); MWWaitFor(() => navigating && navigated && navError, "Navigation didn't take place.");
} }