Progress Event Handlers not firing

Mar 9, 2010 at 6:23 PM

I just started using your DLL and I must say it's pretty awesome. I'm getting inconsistent results with showing the progress during the CompressFiles method. Most of the code has been copied out of the TestForms project in the source code. I've put breakpoints on the _Compressing, _FileCompressionStarted, and _CompressionFinished events, but only the _CompressionFinished event ever fires. I did get this to work in a test program I made, but for the life of me I don't know what is different about my code below versus the test project (which I deleted), or even the GUI example in the source code. Also- any plans to add a CompressDirectoryEncrypted method? Thanks!

Here is the code:

 

 

        delegate void SetProgressDelegate(ProgressEventArgs args);
        delegate void SetFileNameDelegate(FileNameEventArgs args);
        delegate void SetInfoDelegate(FileInfoEventArgs args);
        delegate void SetOverwriteDelegate(FileOverwriteEventArgs args);
        delegate void SetSettings(SevenZipCompressor compr);
        delegate void SetNoArgsDelegate();
        
        string dllPath, archFileName, directory, pwd;

        string[] filesToCompress;

        System.Threading.Thread t;

        public ProgressWindow(string DLLPath, string outputArchive, string[] compressFiles)
        {
            InitializeComponent();

            dllPath = DLLPath;

            archFileName = outputArchive;

            filesToCompress = compressFiles;
        }

        public ProgressWindow(string DLLPath, string outputArchive, string[] compressFiles, string password)
        {
            InitializeComponent();

            dllPath = DLLPath;

            archFileName = outputArchive;

            filesToCompress = compressFiles;

            pwd = password;
        }

        public ProgressWindow(string DLLPath, string outputArchive, string compressDirectory)
        {
            InitializeComponent();

            dllPath = DLLPath;

            archFileName = outputArchive;

            directory = compressDirectory;
        }        

        private void ProgressWindow_Load(object sender, EventArgs e)
        {            
            t = new System.Threading.Thread(new System.Threading.ThreadStart(CompressThread));
            t.Start();
        }        

        void Compress()
        {
            try
            {
                SevenZipCompressor.SetLibraryPath(dllPath);
                SevenZipCompressor cmp = new SevenZipCompressor();
                cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
                cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
                cmp.CompressionFinished += new EventHandler(cmp_CompressionFinished);

                this.Invoke(new SetSettings((compressor) =>
                {
                    compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
                    compressor.CompressionLevel = CompressionLevel.Fast;
                    compressor.FastCompression = true;
                    //compressor.CompressionMethod = CompressionMethod.Lzma2;

                    if (pwd != null && !pwd.Trim().Equals(""))
                    {
                        compressor.EncryptHeaders = true;
                        compressor.ZipEncryptionMethod = ZipEncryptionMethod.Aes256;
                    }
                }), cmp);

                //determine if file is use, if so wait for awhile to see if it frees up, then compress if free
                int seconds = 0;
                while (checkLockedFile())
                {
                    if (seconds == 25)
                    {
                        Helpers.UpdateLogWindowAndScrollDown("E. Files to compress are locked. Skipping compression operation...", false);
                        return;
                    }
                    
                    System.Threading.Thread.Sleep(1000);
                    seconds++;
                }

                if (directory != null && !directory.Equals(""))
                    cmp.CompressDirectory(directory, archFileName);
                else if (filesToCompress != null && filesToCompress.Length > 0)
                {   
                    if (pwd != null && !pwd.Trim().Equals(""))
                        cmp.CompressFilesEncrypted(archFileName, pwd, filesToCompress);
                    else
                        cmp.CompressFiles(archFileName, filesToCompress);
                }
            }
            catch (Exception ex)
            {
                Helpers.UpdateLogWindowAndScrollDown("E. Error compressing file(s): " + ex.Message, false);
            }
            finally
            {
                ThreadHelper.CloseForm(this);
            }
        }

        void cmp_Compressing(object sender, ProgressEventArgs e)
        {
            pb_CompressProgress.Invoke(new SetProgressDelegate((args) =>
            {
                pb_CompressProgress.Increment(args.PercentDelta);
            }), e);
        }

        void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
        {
            l_CompressProgress.Invoke(new SetFileNameDelegate((args) =>
            {
                l_CompressProgress.Text = String.Format("Compressing \"{0}\"", e.FileName);
            }), e);
        }

        void cmp_CompressionFinished(object sender, EventArgs e)
        {
            l_CompressProgress.Invoke(new SetNoArgsDelegate(() =>
            {
                l_CompressProgress.Text = "Finished";
                pb_CompressProgress.Value = 0;
            }));
        }

 

 

Mar 11, 2010 at 5:26 PM
compressor.FastCompression is the culprit.  Set it to false to receive all the event notifications.
Mar 11, 2010 at 5:39 PM

Thank you!

Apr 12, 2010 at 10:53 PM

I'm not seeing the "Compressing" event even without "FastCompression". I'm using the zip format. Is the Compressing event only supported with the LZMA format? Is it only called if the handler was registered on a separate thread from the compress call?

 

Nov 23, 2011 at 8:15 AM
Edited Nov 23, 2011 at 8:50 AM

Is there something like the FastCompression property for extraction/decompression? It seems that by default all events from extraction post only after the whole extraction has finished. That makes it impossible to have a useful progress bar. I'm calling ExtractArchive() and all 100 Extracting events (one for each percentage point) post only after the whole extraction has finished. The test archive is about 1GB, so it's not instant extraction.

Jul 1, 2012 at 2:46 PM
Edited Jul 1, 2012 at 3:17 PM
mihaidi wrote:

Is there something like the FastCompression property for extraction/decompression? It seems that by default all events from extraction post only after the whole extraction has finished. That makes it impossible to have a useful progress bar. I'm calling ExtractArchive() and all 100 Extracting events (one for each percentage point) post only after the whole extraction has finished. The test archive is about 1GB, so it's not instant extraction.

I want to know this too. Maybe needs to be run from another thread?

EDIT: Tried doing it with a BackgroundWorker, didn't help. Can't be arsed to add real multithreaded support without knowing it it would work. Anyone knows anything?

Jan 3, 2013 at 6:03 PM

Hi All,

I am having the same problem with the progress bar, but after I made few changes now I got another error which indicate "ProgressEventArgs' is an ambiguous reference between 'System.Management.ProgressEventArgs' an 'SevenZip.ProgressEventArgs'.  How could I fix this? These are what I got for the namespaces

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.IO.Compression;

using SevenZip.Sdk.Compression.Lzma;

using System.Threading;using System.Management;

using SevenZip;

using System.Text.RegularExpressions;

using System.Globalization;using System.Diagnostics;

using System.Net;

using System.Net.NetworkInformation;

TIA

S

Jan 12, 2013 at 6:25 PM

As you are Using System.Management you will need to add the namespaces to the respective methods. For example

cmp.Compressing += new SevenZip.EventHandler<ProgressEventArgs>(cmp_Compressing);

Let us all know if the event works/fires because no one else seems to be able to get it going properly :-(

 

Jan 12, 2013 at 6:33 PM
LumosX wrote:
mihaidi wrote:

Is there something like the FastCompression property for extraction/decompression? It seems that by default all events from extraction post only after the whole extraction has finished. That makes it impossible to have a useful progress bar. I'm calling ExtractArchive() and all 100 Extracting events (one for each percentage point) post only after the whole extraction has finished. The test archive is about 1GB, so it's not instant extraction.

I want to know this too. Maybe needs to be run from another thread?

EDIT: Tried doing it with a BackgroundWorker, didn't help. Can't be arsed to add real multithreaded support without knowing it it would work. Anyone knows anything?

Did you ever get this working?

I've tried everything I can think of including calling the extraction using a  backgroundworker  and then having the extract.progress event pass the appropriate data to the backgroundworker.progress event but that doesn't work either. As the extract is performed asynchronously, you can't even pause the master thread until extraction is completed!!

Normally I would have moved on by now but 7zip and this library are just to fast and effcient to not want to get it working.

 

Jan 14, 2013 at 2:52 PM

Hi glenharvy,

Adding cmp.Compressing += new EventHandler<SevenZip.ProgressEventArgs>(cmp_Compressing);

remove the ambiguous reference error, but the progress bar is still not firing.  Surprisingly, the cmp_FilecompressionStarted is working.  Any suggestions?

The code below for the compression section taken from the sample

cmp.Compressing += new EventHandler<SevenZip.ProgressEventArgs>(cmp_Compressing);               

cmp.FileCompressionStarted += new EventHandler<SevenZip.FileNameEventArgs>(cmp_FileCompressionStarted);               

cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompressionFinished);                              


cmp.ArchiveFormat = SevenZip.OutArchiveFormat.Zip;               

cmp.CompressionLevel = SevenZip.CompressionLevel.Ultra;               

cmp.FastCompression = false;

 

void cmp_Compressing(object sender, SevenZip.ProgressEventArgs e)       

{           

    progressBarUpload.Style = ProgressBarStyle.Marquee;           

    progressBarUpload.Increment(e.PercentDone);           

}

 

Let me know if I am missing anything to prevent the progress bar from firing.  Right now, while compression is working in the background, my windows form is showing "Not Responding" and this might give the user a wrong impression.

TIA.

Jan 14, 2013 at 6:32 PM
Edited Jan 14, 2013 at 6:32 PM
slau108 wrote:

Hi glenharvy,

Adding cmp.Compressing += new EventHandler<SevenZip.ProgressEventArgs>(cmp_Compressing);

Adding cmp.Compressing += new SevenZip.EventHandler<ProgressEventArgs>(cmp_Compressing);

is what it should be I believe.

Have a look at my later separate post on a working c# example.

Jan 14, 2013 at 7:28 PM

Hi genharvy,

EventHandler does not exist in SevenZip namespace.  Thanks for looking into your c# example.

Jan 14, 2013 at 7:59 PM
slau108 wrote:

Hi genharvy,

EventHandler does not exist in SevenZip namespace.  Thanks for looking into your c# example.

Yes, sorry about that.