Reputation: 1923
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.
Upvotes: 0
Views: 189
Reputation: 2159
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.
e.g :
await InternetConnectionProfile.GetNetworkUsageAsync(...);
//The content of GetNetworkUsageAsyncHandler can be moved here.
I hope it turns out to be helpful.
Upvotes: 2
Reputation: 1923
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.
Upvotes: 0