question

VincentWang-4302 avatar image
0 Votes"
VincentWang-4302 asked ·

How to get the real-time network speed?

I used to get the data usage and divide the second ,but the result is always 0.

 private async Task GetSpeed()
         {
             ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
             var currentDate = DateTime.Now;
             var startDate = DateTime.Now.AddSeconds(-2);
 
             NetworkUsageStates NetworkUsageStates = new NetworkUsageStates();
             NetworkUsageStates.Roaming = TriStates.No;
             NetworkUsageStates.Shared = TriStates.No;
 
             // dierctly using  NetworkUsageStates object without setting roaming and shared property will get null result.
             //var networkUsage = await internetConnectionProfile.GetNetworkUsageAsync(utcTimeA, utcTimeB, DataUsageGranularity.Total, new NetworkUsageStates()) ;
             var networkUsage = await internetConnectionProfile.GetNetworkUsageAsync(startDate, currentDate, DataUsageGranularity.Total, NetworkUsageStates);
             foreach (var usage in networkUsage)
             {
                 var received = usage.BytesReceived / 2 / 1000000;
                 var sent = usage.BytesSent / 2 / 1000000;
                 tb.Text = "Down:" + received + "Mb " + "Up: " + sent + "Mb";
             }
         }

So how to get the real-time network speed?

uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

Did you use a network proxy such as a VPN when testing? In my test, normal traffic data cannot be obtained when the VPN is enabled.

Maybe you can try another special method to connect with StreamSocket for downlink speed:

 public async Task GetNetworkSpeed()
 {
     double currentSpeed = 0;
     var socket = new StreamSocket();
     socket.Control.NoDelay = true;
     socket.Control.QualityOfService = SocketQualityOfService.LowLatency;
     socket.Control.KeepAlive = false;
     using (socket)
     {
         await socket.ConnectAsync(new HostName("bing.com"), "80", SocketProtectionLevel.PlainSocket);
         currentSpeed = socket.Information.RoundTripTimeStatistics.Min / 1000000.0;
     }
     return currentSpeed;
 }

Thanks.

· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, my pc does not uses vpn. But our network is HK line, it uses vpn to connect the outer web. But if I set a longer time duration, like 1 day, the networkUsage has data. So it seems like ConnectionProfile is not real-time.

And your solution is to test the connect speed to Bing. If I download a big file, pc net monitor shows download speed is 11MB/s, your solution is still not match.

Thanks.

0 Votes 0 · ·

Hello, I'm sorry for the delay. This may be a UWP application's move to reduce system resource consumption. No good way has been found to solve this problem. If you want to download files, you can consider using BackgroundDownloader, which provides some methods about download speed, here is the documentation

0 Votes 0 · ·

Thanks for the reply.

0 Votes 0 · ·
Show more comments