Skip to content

Commit

Permalink
1. Changed the implemenation of the Interpreter.MaximumReconnectionAt…
Browse files Browse the repository at this point in the history
…tempts property so that it only counts *consecutive* reconnection attempts. A successful reconnection now starts the count over again at zero.

2. Fixed a bug in the Devices class that sometimes caused NullReferenceExceptions
  • Loading branch information
JamesMessinger committed Dec 15, 2009
1 parent 8a709eb commit 4538384
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
7 changes: 5 additions & 2 deletions Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,13 @@ private static void WaitForDetectionInternal()
try
{
ManualResetEvent handle = _CurrentlyDetectingWaitHandles[0];
if (handle != null)
{
#if !PocketPC
if (!handle.SafeWaitHandle.IsClosed)
if (!handle.SafeWaitHandle.IsClosed)
#endif
handle.WaitOne();
handle.WaitOne();
}
}
catch (ObjectDisposedException)
{
Expand Down
70 changes: 46 additions & 24 deletions Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ public DilutionOfPrecision MaximumHorizontalDilutionOfPrecision
}

/// <summary>
/// Controls the maximum number of reconnection retries allowed.
/// Controls the maximum number of consecutive reconnection retries allowed.
/// </summary>
#if !PocketPC
[Category("Behavior")]
[Description("Controls the maximum number of reconnection retries allowed.")]
[Description("Controls the maximum number of consecutive reconnection retries allowed.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
Expand Down Expand Up @@ -1889,7 +1889,12 @@ private void ParsingThreadProc()

// If it's null then wait a while and try again
if (_Device == null)
continue;
{
if (QueryReconnectAllowed())
continue;
else
return;
}

// Signal that we're starting
OnStarting();
Expand All @@ -1899,6 +1904,10 @@ private void ParsingThreadProc()

// And we're started
OnStarted();

// Reset the reconnection counter, since we only care about consecutive reconnects
_ReconnectionAttemptCount = 0;

continue;
}

Expand Down Expand Up @@ -1930,30 +1939,10 @@ private void ParsingThreadProc()
OnStopped();

// Are we automatically reconnecting?
if (_AllowAutomaticReconnection)
{
/* Wait just a moment before trying again. This gives software such as the Bluetooth stack
* the ability to properly reset and make connections again.
*/

// Bump up the failure count
_ReconnectionAttemptCount++;

// If we're above the maximum, stop trying to reconnect
if (_MaximumReconnectionAttempts > -1
&& _ReconnectionAttemptCount >= _MaximumReconnectionAttempts)
{
// Exit
return;
}

if (QueryReconnectAllowed())
continue;
}
else
{
// Automatic reconnection is not allowed, so exit
return;
}
}
catch (Exception ex)
{
Expand All @@ -1974,6 +1963,39 @@ private void ParsingThreadProc()
#endif
}

/// <summary>
/// Determines if automatic reconnection is currently allowed, based on the values of
/// <see cref="AllowAutomaticReconnection"/>, <see cref="MaximumReconnectionAttempts"/>, and <see cref="_ReconnectionAttemptCount"/>.
/// If reconnection is allowed, then <see cref="_ReconnectionAttemptCount"/> is incremented after a short delay.
/// </summary>
/// <returns>
/// <see langword="true">True</see> if another reconnection attempt should be made; otherwise, <see langword="false"/>.
/// </returns>
private bool QueryReconnectAllowed()
{
// Are we automatically reconnecting?
if (_AllowAutomaticReconnection)
{
// Determine if we've exceeded the maximum reconnects
if (_MaximumReconnectionAttempts == -1
|| _ReconnectionAttemptCount < _MaximumReconnectionAttempts)
{
/* Wait just a moment before reconnecting. This gives software such as the Bluetooth stack
* the ability to properly reset and make connections again.
*/
Thread.Sleep(1000);

// Bump up the failure count
_ReconnectionAttemptCount++;

return true;
}
}

// Automatic reconnection is not allowed, or we have exceeded the maximum reconnects
return false;
}

#endregion
}
}

0 comments on commit 4538384

Please sign in to comment.