Skip to content

Commit

Permalink
Test Connection Async
Browse files Browse the repository at this point in the history
  • Loading branch information
nertsch committed Sep 10, 2018
1 parent 56daf98 commit 7179fb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 11 additions & 11 deletions CalDavSynchronizer/Scheduling/SynchronizationProfileRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,19 @@ public async Task RunAndRescheduleNoThrow (bool wasManuallyTriggered)
}
}

private async Task RunAllPendingJobs ()
private async Task RunAllPendingJobs()
{
if (_profile.CheckIfOnline && !ConnectionTester.IsOnline (_profile.ProxyOptionsOrNull))
{
s_logger.WarnFormat ("Skipping synchronization profile '{0}' (Id: '{1}') because network is not available", _profile.ProfileName, _profileId);
return;
}

// Monitor cannot be used here, since Monitor allows recursive enter for a thread
if (Interlocked.CompareExchange (ref _isRunning, 1, 0) == 0)
if (Interlocked.CompareExchange(ref _isRunning, 1, 0) == 0)
{
try
{
if (_profile.CheckIfOnline && !await ConnectionTester.IsOnline(_profile.ProxyOptionsOrNull))
{
s_logger.WarnFormat("Skipping synchronization profile '{0}' (Id: '{1}') because network is not available", _profile.ProfileName, _profileId);
return;
}

while (_fullSyncPending || _pendingOutlookItems.Count > 0)
{
if (_fullSyncPending)
Expand All @@ -238,16 +238,16 @@ private async Task RunAllPendingJobs ()
_pendingOutlookItems.Clear();
if (s_logger.IsDebugEnabled)
{
s_logger.Debug ($"Partial sync: Going to sync '{itemsToSync.Length}' pending items ( {string.Join (", ", itemsToSync.Select (id => id.EntryId))} ).");
s_logger.Debug($"Partial sync: Going to sync '{itemsToSync.Length}' pending items ( {string.Join(", ", itemsToSync.Select(id => id.EntryId))} ).");
}
Thread.MemoryBarrier(); // should not be required because there is just one thread entering multiple times
await RunPartialNoThrow (itemsToSync);
await RunPartialNoThrow(itemsToSync);
}
}
}
finally
{
Interlocked.Exchange (ref _isRunning, 0);
Interlocked.Exchange(ref _isRunning, 0);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions CalDavSynchronizer/Ui/ConnectionTests/ConnectionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace CalDavSynchronizer.Ui.ConnectionTests
{
public static class ConnectionTester
{
public static bool IsOnline (ProxyOptions proxyOptionsOrNull)
public static async Task<bool> IsOnline (ProxyOptions proxyOptionsOrNull)
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
// Use NCSI to detect network status according to https://technet.microsoft.com/en-us/library/ee126135(v=WS.10).aspx
// try DNS first
try
{
IPHostEntry hostEntry = Dns.GetHostEntry ("dns.msftncsi.com");
IPHostEntry hostEntry = await Dns.GetHostEntryAsync ("dns.msftncsi.com");
IPAddress ipAddress = Array.Find (hostEntry.AddressList, ip => ip.AddressFamily == AddressFamily.InterNetwork);
if (ipAddress != null && ipAddress.ToString() == "131.107.255.255") return true;
}
Expand All @@ -48,7 +48,7 @@ public static bool IsOnline (ProxyOptions proxyOptionsOrNull)
{
IWebProxy proxy = (proxyOptionsOrNull != null) ? SynchronizerFactory.CreateProxy (proxyOptionsOrNull) : null;
client.Proxy = proxy;
txt = client.DownloadString (new Uri ("http://www.msftncsi.com/ncsi.txt"));
txt = await client.DownloadStringTaskAsync (new Uri ("http://www.msftncsi.com/ncsi.txt"));
}
if (txt != "Microsoft NCSI") return false;
return true;
Expand Down

0 comments on commit 7179fb7

Please sign in to comment.