流量跟踪WinRT UWP窗口10

时间:2016-04-08 15:35:36

标签: c# .net asynchronous windows-runtime

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

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;
        }

我将此metode作为对象的参数

ConnectionModel model = new ConnectionModel(BytesReceivedCheck(connectionProfile));

这是我的模特

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();中收到了在意外时间调用的exceprion“方法”。我认为Async的问题,请给我这个方法的正确例子。我是个乞丐,对于可能愚蠢的问题感到抱歉。

0 个答案:

没有答案