Skip to content

Commit

Permalink
Fixed thread safety for NetworkHelper.Instance (#3490)
Browse files Browse the repository at this point in the history
## Fixes #3197
<!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. -->

<!-- Add a brief overview here of the feature/bug & fix. -->

## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->

- Bugfix
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
The `NetworkHelper.Instance` property is not thread-safe.

## What is the new behavior?
<!-- Describe how was this issue resolved or changed? -->
The `NetworkHelper.Instance` property is now thread safe and initialized in the implicit static constructor.

## PR Checklist

Please check if your PR fulfills the following requirements:

- [X] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~
- [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~
    - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~
- [X] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [ ] Contains **NO** breaking changes
  • Loading branch information
msftbot[bot] authored Nov 6, 2020
2 parents 75753e5 + 2fd9b38 commit 43693f8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Microsoft.Toolkit.Uwp.Connectivity/Network/NetworkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,11 @@ namespace Microsoft.Toolkit.Uwp.Connectivity
/// </summary>
public class NetworkHelper
{
/// <summary>
/// Private singleton field.
/// </summary>
private static NetworkHelper _instance;

/// <summary>
/// Event raised when the network changes.
/// </summary>
public event EventHandler NetworkChanged;

/// <summary>
/// Gets public singleton property.
/// </summary>
public static NetworkHelper Instance => _instance ?? (_instance = new NetworkHelper());

/// <summary>
/// Gets instance of <see cref="ConnectionInformation"/>.
/// </summary>
public ConnectionInformation ConnectionInformation { get; } = new ConnectionInformation();

/// <summary>
/// Initializes a new instance of the <see cref="NetworkHelper"/> class.
/// </summary>
Expand All @@ -52,6 +37,19 @@ protected NetworkHelper()
NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChanged;
}

/// <summary>
/// Gets public singleton property.
/// </summary>
public static NetworkHelper Instance { get; } = new NetworkHelper();

/// <summary>
/// Gets instance of <see cref="ConnectionInformation"/>.
/// </summary>
public ConnectionInformation ConnectionInformation { get; }

/// <summary>
/// Checks the current connection information and raises <see cref="NetworkChanged"/> if needed.
/// </summary>
private void UpdateConnectionInformation()
{
lock (ConnectionInformation)
Expand All @@ -69,6 +67,9 @@ private void UpdateConnectionInformation()
}
}

/// <summary>
/// Invokes <see cref="UpdateConnectionInformation"/> when the current network status changes.
/// </summary>
private void OnNetworkStatusChanged(object sender)
{
UpdateConnectionInformation();
Expand Down

0 comments on commit 43693f8

Please sign in to comment.