Progressbar with BackgroundWorker

Aug 19, 2009 at 6:42 AM

Hi,

first of all many thanks for the great work on DotNetZip. It's a really great Libary to work with.But i'm new to C# an i wonder if there is a way to use a progressbar while the ZIP work is done by a BackgroundWorker. I Found exsamples only for working with threats and not for backgroundworker.

I'm looking forward for any hints and sorry for my bad english :-)

 

Andreas

Coordinator
Aug 19, 2009 at 6:57 AM

I think it's the same idea.

If you want to plug into the events from ZipFile in order to use that information for progress updates, you will need to register handlers for the ExtractProgress, SaveProgress or ReadProgress events on the ZipFile.  Those events will be invoked on the BackgroundWorker thread.  If want to update elements of the UI, like a progressbar, directly from that thread, you'll need to do the InvokeRequired dance.  You could call the ProgressChanged event from within the {Extract, Save, Read} Progress event handler, and then update the Progressbar within the ProgressChanged event handler.  In that case you won't need the InvokeRequired stuff.  That's the whole point of the BackgroundWorker, I Think. 

But to do that, you will have to wire up the ExtractProgress (Etc) on ZipFile, to the ProgressChanged event for BackgroundWorker.  Not too complicated, but not simple.

What is the reason you are interested in BackgroundWorker, anyway?

 

Aug 19, 2009 at 7:31 AM

Thanks you for the fast reply.

I'm using backgroundworker because it looked easier for me. I don't need multiple threats.

Where can i find information about registering handlers for the SaveProgress?

Andreas

Aug 19, 2009 at 8:30 AM
Edited Aug 19, 2009 at 9:25 AM

Now i got the handlers working. Now i don't have any idea how to get the percentage of how much work is done on zipping. I've got it with count of files, but is there a way to do it on filesize? I allways have pretty large files at the beginning and a lot of small files at the end. So the progressbar doesn't show the real progress. Any ideas on this?

 

Andreas

Coordinator
Aug 19, 2009 at 1:51 PM

There are working exmples in VB and C# available at

http://code.msdn.microsoft.com/DotNetZip

They use progressbars.  Also the doc on SaveProgress shows how to compute percentages.

Aug 19, 2009 at 8:54 PM

Thank you for your help. I didn't found a simple way to do the progressbar on total bytes of all files. So I created two progressbars, one with the filecount and the second is showing the the progress of the actual file. Thats ok for me :-)

Thank you again for your great work and fast support.

Andreas

Coordinator
Aug 19, 2009 at 11:50 PM

To do a single progressbar on the total bytes for all files, you'd have to compute a total of all the UncompressedSize for all files being extracted or saved, and then keep a running total of the number of bytes done so far.  Update the running total in the progress event.