Share via


ConnectivityInterval.ConnectionDuration Property

Definition

Length of this connectivity interval.

public:
 property TimeSpan ConnectionDuration { TimeSpan get(); };
TimeSpan ConnectionDuration();
public System.TimeSpan ConnectionDuration { get; }
var timeSpan = connectivityInterval.connectionDuration;
Public ReadOnly Property ConnectionDuration As TimeSpan

Property Value

A Windows.Foundation.TimeSpan representing how long the interface remained connected during this interval. May be shorter than the actual live connection if the queried window started or ended mid-interval.

Examples

var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (profile != null)
{
    var end = DateTime.UtcNow;
    var start = end - TimeSpan.FromHours(1);
    var states = new Windows.Networking.Connectivity.NetworkUsageStates();
    var intervals = await profile.GetConnectivityIntervalsAsync(start, end, states);

    double connectedSeconds = intervals.Sum(iv => iv.ConnectionDuration.TotalSeconds);
    double utilization = connectedSeconds / (end - start).TotalSeconds;
    // utilization now reflects fraction of the hour the interface was connected
}

Remarks

Notes

  • Pair with ConnectivityInterval.StartTime to identify the full span.
  • Does not imply any data transfer; combine with usage buckets from ConnectionProfile.GetNetworkUsageAsync for volume metrics.
  • Can be truncated when your query window (start/end) slices through an active connection.
  • Multiple intervals for a profile can have gaps (device sleep, transition, disconnect).
  • Summing all ConnectionDuration values from a query window yields total connected time; divide by wall-clock span to compute utilization.
  • For incremental processing, persist the end (StartTime + ConnectionDuration) of the last fully closed interval and resume from there to avoid recounting an interval still in progress.

Tip

Treat very short durations (for example a few milliseconds) cautiously - brief disconnect/reconnect events may be coalesced by higher level logic and are rarely meaningful for end-user analytics.

Applies to