Search code examples
c#multithreadinginheritanceprogress-bardispose

Disposed Forms with a Base and threading returns null for progress bar


I have a base form that I use when calling 2 forms. Previously when calling the forms I didn't dispose of them, but I have found that reusing them, they would stay in memory and not get collected. So I have instead used a using statement instead to clear the memory, and all my problem are fixed.

But now a new problem arises, one that I had previously when testing my app with mono on Linux. I though it might be a mono specific problem, but since adding the using statement the same thing happens on my Windows machine. So it might just be that the Garbage Collector on Mono is different and was disposing properly of my forms.

Here is my problem I have a thread that I start to extract files in the background And I have progress bar telling me the progress, before using the dispose if I closed the form and reopened it my files extracted correctly and the progress bar was working fine. But now they work fine the first time, but if I reopen the form or the other one that has the same base, the extraction is not working, no files are extracted because I have a null exception when reporting the progress.

    private void ExtractFiles()
    {
        Zip.ExtractProgress += new EventHandler<ExtractProgressArgs>(Utils_ExtractProgress);
        Thread t = new Thread(new ThreadStart(Zip.ExtractZip));
        t.IsBackground = true;
        t.Start();
        FilesExtracted = true;
    }

    void Utils_ExtractProgress(object sender, ExtractProgressArgs e)
    {
        UpdateProgress(e.Pourcentage);
    }

    private delegate void UpdateProgressDelegate(int Pourc);
    private void UpdateProgress(int Pourc)
    {
        lock (this)
        {
            if (Progress.ProgressBar.InvokeRequired)
            {
                UpdateProgressDelegate del = new UpdateProgressDelegate(UpdateProgress);
                Progress.ProgressBar.BeginInvoke(del, Pourc);
            } else
            {
                Progress.Value = Pourc;

            }
        }
    }

This code is in my BaseForm, the Progress control isn't null, but all of it's properties have null exceptions. So when checking if Invoked is required it raises an Null exception.

Here is my Zip.Extract method

    public static event EventHandler<ExtractProgressArgs> ExtractProgress;
    static ExtractProgressArgs Progress;

    internal static void ExtractZip()
    {
        try
        {
            using (ZipFile zip = ZipFile.Read(Variables.Filename))
            {
                Progress = new ExtractProgressArgs();
                Progress.TotalToTransfer = Convert.ToInt32(zip.Sum(e => e.UncompressedSize));
                zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
                Old = 0; New = 0;
                foreach (ZipEntry item in zip)
                {

                    item.Extract(Variables.TempFolder, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        } catch (Exception)
        {
        }
    }

    static long Old;
    static long New;
    static void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
    {
        if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
        {
            New = e.BytesTransferred;
            Progress.Transferred += New - Old;
            Old = e.BytesTransferred;

            if (ExtractProgress != null)
            {
                ExtractProgress(e.CurrentEntry, Progress);
            }

        } else if (e.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
        {
            Old = 0;
        }
    }

Might be because my Zip.Extract is static? I have almost no knowledge of multi-threading, like synchronization, etc.


Solution

  • The short answer is yes, some of your problems are due to the static nature of those operations.

    You should be able to resolve the problem by removing the static declarations from your Zip class and then creating an instance of it as needed.