开发者

how to check how many bits in a byte array?

Im creating a download speed test, and im downloading a 800megabit file to a Byte[] in a memory stream with

webClient.DownloadData开发者_StackOverflowAsync(new Uri(link), memStreamArray); 

How can i check how many bits are in the memStreamArray while downloading? I need this so i can do a calculation on size / time to get the speed in realtime.

Im planing on performing this calculation in the webClient.DownloadProgressChanged event.


You need a DownloadProgressChanged event handler for this. The second parameter in the DownloadDataAsync method is just an object you can retrieve in your callback UserState variable, it's just pass-through - you probably have no use for it in your scenario.

WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine(e.BytesReceived);
};

webClient.DownloadDataAsync(new Uri(link));


1 Byte = 8 bits and you have a byte arrray. Count how many bytes to you have in the array an multiply by 8. Or is it a trick question?


Not sure on the thread safety of this, but you can check the Length property of the memory stream. That will give the length in bytes, then just convert to bits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜