简体   繁体   English

Windows Phone 8.1(C#)中的GetNetworkUsageAsync()

[英]GetNetworkUsageAsync() in windows Phone 8.1 (C#)

I want to use GetNetworkUsageAsync to get bytes send and receive. 我想使用GetNetworkUsageAsync来获取发送和接收的字节。
When I do like this, GetNetworkUsageAsyncHandler called But not immediately. 当我这样做时,GetNetworkUsageAsyncHandler调用了But,但不会立即进行。
I want to use BytesSent and BytesReceived immediately. 我想立即使用BytesSent和BytesReceived。
What can I do? 我能做什么?

private void MakeDataUsageChart()
    {
        //Make days of Month
        GregorianDates = MakeMonthGregorianDate(YearGregorian, MonthGregorian);

        localTime = new DateTime(2015, 9, 2, 0, 0, 0);
        StartTime = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));
        localTime = new DateTime(2015, 9, 3, 0, 0, 0);
        EndTime = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));
        InternetConnectionProfile.GetNetworkUsageAsync(StartTime, EndTime, DataUsageGranularity.Total, new NetworkUsageStates()).Completed = GetNetworkUsageAsyncHandler;

        for (int count = 0 ; count <= GregorianDates.Count -1 ; count++)
        {
            ChartDatasGregorian.Add(new Data()
            {
                CategoryDownload = GregorianDates[count],
                ValueDownload = BytesReceived,
                CategoryUpload = GregorianDates[count],
                ValueUpload = BytesSent,
                CategoryTotal = GregorianDates[count],
                ValueTotal = BytesReceived + BytesSent
            });
        }
        this.DataUsageLineSeries.DataContext = ChartDatasGregorian;
    }
    private void GetNetworkUsageAsyncHandler(IAsyncOperation<IReadOnlyList<NetworkUsage>> asyncInfo, AsyncStatus asyncStatus)
    {
        if (asyncStatus == AsyncStatus.Completed)
        {
            IReadOnlyList<NetworkUsage> networkUsages = asyncInfo.GetResults();

            foreach (NetworkUsage networkUsage in networkUsages)
            {
                string BytesSents = networkUsage.BytesSent.ToString();
                string BytesReceiveds = networkUsage.BytesReceived.ToString();
                BytesSent = Convert.ToInt64(BytesSents);
                BytesReceived = Convert.ToInt64(BytesReceiveds);

                //Column2DataPlan.Text = BytesSents + "   " + BytesReceiveds;
            }

        }
    }

I'm sorry for bad English. 对不起,英语不好。
Thanks. 谢谢。

As far as I'm concerned, instead of calling an awaitable method and assigning the Completed event, It's better to call the method using await operator and continue your desired logic afterwards. 就我而言,与其调用一个等待方法并分配Completed事件,不如使用await运算符调用该方法并随后继续执行所需的逻辑,这更好。

eg : 例如:

await InternetConnectionProfile.GetNetworkUsageAsync(...);
//The content of GetNetworkUsageAsyncHandler can be moved here.

I hope it turns out to be helpful. 希望对您有所帮助。

Ok. 好。 I want to say the answer of question. 我想说问题的答案。 Thank you Mohammad Chamanpara a lot. 非常感谢Mohammad Chamanpara
I just changed it a little: 我只是稍微改变了一下:

IReadOnlyList<NetworkUsage> asyncInfo = await InternetConnectionProfile.GetNetworkUsageAsync(StartTime, EndTime, DataUsageGranularity.Total, new NetworkUsageStates());

        foreach (NetworkUsage networkUsage in asyncInfo)
        {
            string BytesSents = networkUsage.BytesSent.ToString();
            string BytesReceiveds = networkUsage.BytesReceived.ToString();
            BytesSent = Convert.ToInt64(BytesSents) / 1024 / 1024;
            BytesReceived = Convert.ToInt64(BytesReceiveds) / 1024 / 1024;
        }

I hope that this help everyone. 希望对大家有帮助。
Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM