簡體   English   中英

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

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

我想使用GetNetworkUsageAsync來獲取發送和接收的字節。
當我這樣做時,GetNetworkUsageAsyncHandler調用了But,但不會立即進行。
我想立即使用BytesSent和BytesReceived。
我能做什么?

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

        }
    }

對不起,英語不好。
謝謝。

就我而言,與其調用一個等待方法並分配Completed事件,不如使用await運算符調用該方法並隨后繼續執行所需的邏輯,這更好。

例如:

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

希望對您有所幫助。

好。 我想說問題的答案。 非常感謝Mohammad Chamanpara
我只是稍微改變了一下:

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

希望對大家有幫助。
謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM