简体   繁体   中英

GetNetworkUsageAsync() in windows Phone 8.1 (C#)

I want to use GetNetworkUsageAsync to get bytes send and receive.
When I do like this, GetNetworkUsageAsyncHandler called But not immediately.
I want to use BytesSent and BytesReceived immediately.
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.

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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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