Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application that reads binary files byte by byte and report reading progress in a ProgressBar.

I read the file in another thread using a BackgroundWorker and I update the UI using ReportProgres.

My problem is that the application runs very slow because of ReportProgres. For a 10 MB file takes about 3 minutes, if I comment the ReportProgress line, takes 2 seconds.

Any ideas about this?
Posted
Comments
Herman<T>.Instance 28-Feb-12 10:43am    
why byte by byte? A user doesn't see's thje difference between 1 byte and 1 kilobyte!

If you update the ProgressBar on every byte handled then it will indeed be very slow. Calls to UI elements are very expensive to a certain degree which will become very noticeable when called very often (also because the call needs to be invoked into the user thread). A simple way is to check if there is actually any significant progress to report and only then report it. You can use all kind off advanced schemes for this but for the sake of testing, simply use something like:
If((current_byte_handled And 127) = 127) Then
  ReportProgres ...
End If


This will limit the number of calls and should make it faster already.

Good luck!
 
Share this answer
 
Do we really need to answer?

If you try to send notification byte-by-byte, you perform 10,485,760 invocations. Reading of one byte takes no time compared to one invocation. The invocation packs the delegate instance with all the data required to its call with parameters, queues this packet in the UI thread invocation queue and either returns (in case of BeginInvoke) or waits until the UI thread remove this package from the queue, performs the actual call using UI controls, processes events and notifies the thread calling Invoke. Do you want to measure the overhead of such notification compared to reading a byte? Perhaps you already can estimate it.

So, does the notification on each byte make any sense? Just think about it.

For more detail on invocation, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900