C# 流量跟踪WinRT UWP windows 10

C# 流量跟踪WinRT UWP windows 10,c#,.net,asynchronous,windows-runtime,C#,.net,Asynchronous,Windows Runtime,尝试在PC上创建的每个连接中接收有关已接收和已发送字节的数据。我正在使用WinRT UWP windows 10app,名称空间windows.Networking.Connectivity。 这是我的方法 private ulong BytesReceivedCheck(ConnectionProfile connectionProfile) { var currentDate = DateTime.Now; var startD

尝试在PC上创建的每个连接中接收有关已接收和已发送字节的数据。我正在使用WinRT UWP windows 10app,名称空间windows.Networking.Connectivity。 这是我的方法

private ulong BytesReceivedCheck(ConnectionProfile connectionProfile)
        {
            var currentDate = DateTime.Now;
            var startDate = DateTime.Now.AddYears(-1);
            //Get the ConnectionProfile that is currently used to connect to the Internet
            IAsyncOperation<IReadOnlyList<NetworkUsage>> LocalUsage = connectionProfile.GetNetworkUsageAsync(startDate, currentDate, DataUsageGranularity.Total, new NetworkUsageStates());
            var networkUsages = LocalUsage.GetResults();
            foreach (NetworkUsage networkUsage in networkUsages)
            {
                return networkUsage.BytesReceived;
            }
            return 0;
        }
这是我的模型

public class ConnectionModel : INotifyPropertyChanged
    {
ulong bytesReceived;
public ulong BytesReceived
        {
            get
            {
                return this.bytesReceived;
            }
            set
            {
                if (this.bytesReceived != value)
                {
                    this.bytesReceived = value;
                    this.RaisePropertyChanged();
                }
            }
        }
public ConnectionModel(ulong bytesReceived)
        {
            this.BytesReceived = bytesReceived;
}
}

我在
var networkUsages=LocalUsage.GetResults()行中收到异常“在意外时间调用的方法”在我的方法中。我认为异步的问题,请给我这个方法的正确例子。我是一个乞丐,很抱歉问了一个愚蠢的问题。

使用
wait
关键字等待结果准备就绪。使用
wait
关键字等待结果准备就绪。
public class ConnectionModel : INotifyPropertyChanged
    {
ulong bytesReceived;
public ulong BytesReceived
        {
            get
            {
                return this.bytesReceived;
            }
            set
            {
                if (this.bytesReceived != value)
                {
                    this.bytesReceived = value;
                    this.RaisePropertyChanged();
                }
            }
        }
public ConnectionModel(ulong bytesReceived)
        {
            this.BytesReceived = bytesReceived;
}
}