newandfresh
newandfresh

Reputation: 309

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

Upvotes: 2

Views: 2052

Answers (3)

Liviu Mandras
Liviu Mandras

Reputation: 6627

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?

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 161012

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

Upvotes: 3

pstrjds
pstrjds

Reputation: 17448

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.

Upvotes: 1

Related Questions