• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Selfclosing form when unpacking is done

Hi,

once more I've a problem with a form that should be closed after an archive had been unpacked.
Anything works fine until the form close itself it reports an exception.
It will be displayed like this:
Entpacken elf=new Entpacken(source, target);
elf.ShowDialog();

How to solf this problem?

Thanks

Andre
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.Threading;
namespace UpdateLib
{
    public partial class Entpacken : Form
    {

        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

        private BackgroundWorker bgw = new System.ComponentModel.BackgroundWorker();
        private string FileName="";
        private string OutputDir="";

        public Entpacken(string _FileName, string _OutputDir)
        {
            FileName = _FileName;
            OutputDir = _OutputDir;
            InitializeComponent();
            bgw.WorkerReportsProgress = true;
            bgw.DoWork += new System.ComponentModel.DoWorkEventHandler(bgw_DoWork);
            bgw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bgw_ProgressChanged);
            bgw.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);

            this.Shown += new EventHandler(Entpacken_Shown);            

        }

        private void Entpacken_Shown(object sender, EventArgs e)
        {
            bgw.RunWorkerAsync();
        }

        void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                ZipObj zo = new ZipObj();
                FileStream ZFS = new FileStream(FileName, FileMode.Open);
                ICSharpCode.SharpZipLib.Zip.ZipInputStream ZIN = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ZFS);
                
                ICSharpCode.SharpZipLib.Zip.ZipEntry ZipEntry = default(ICSharpCode.SharpZipLib.Zip.ZipEntry);
                
                byte[] Buffer = new byte[4097];
                int ByteLen = 0;
                FileStream FS = null;

                string InZipDirName = null;
                string InZipFileName = null;
                string TargetFileName = null;
                zo.ASize = ZFS.Length;
                do
                {
                    ZipEntry = ZIN.GetNextEntry();

                    if (ZipEntry == null) break;
                    // WriteLog("Entpacke: " + ZipEntry.Name);
                    InZipDirName = Path.GetDirectoryName(ZipEntry.Name) + "\\";
                    InZipFileName = Path.GetFileName(ZipEntry.Name);
                    zo.FName = InZipFileName;
                    zo.FSize = ZipEntry.Size;
                    bgw.ReportProgress(-1, zo);
                    long cpos = 0;
                    if (!Directory.Exists(Path.Combine(OutputDir, InZipDirName))) Directory.CreateDirectory(Path.Combine(OutputDir, InZipDirName));
                    if (!String.IsNullOrEmpty(InZipFileName))
                    {
                        if (InZipDirName == "\\") InZipDirName = "";
                        TargetFileName = OutputDir + "\\" + InZipDirName + InZipFileName;

                        FS = new FileStream(TargetFileName, FileMode.Create);
                        do
                        {
                            ByteLen = ZIN.Read(Buffer, 0, Buffer.Length);
                            FS.Write(Buffer, 0, ByteLen);
                            if (ByteLen > 0)
                            {
                                cpos += ByteLen;
                                bgw.ReportProgress((int)(zo.FSize / cpos * 100), null);
                            }
                        }
                        while (!(ByteLen <= 0));
                        FS.Close();
                    }
                }
                while (true);

                ZIN.Close();
                ZFS.Close();
            }
            catch (Exception ex)
            {
                UpdateMain.WriteLog("Fehler: " + ex.Message);
            }
        }

        void bgw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.UserState == null)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
            else
            {
                ZipObj zo = (ZipObj)e.UserState;
                label1.Text = zo.FName;
                progressBar1.Value = 0;
            }
        }

        void bgw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
       
    }

    class ZipObj
    {
        private long _FSize;
        private long _ASize;
        private long _CPos;
        private string _FName;

        public long FSize
        {
            get { return this._FSize; }
            set { this._FSize = value; }
        }

        public long ASize
        {
            get { return this._ASize; }
            set { this._ASize = value; }
        }

        public long CPos
        {
            get { return this._CPos; }
            set { this._CPos = value; }
        }

        public string FName
        {
            get { return this._FName; }
            set { this._FName = value; }
        }
    }
}

Open in new window

0
andre72
Asked:
andre72
  • 5
  • 3
1 Solution
 
SStoryCommented:
What's the exception? Are you closing the form before work is finished? If so I guess you are getting a thread abort exception.  You probably want to make the main thread wait on the other thread before shutting down.
0
 
andre72Author Commented:
No the extracting of the archive is complete than ...
It's an TargetInvocationException I get.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The basic model you've proposed should work...I tested it with the below code and it worked as expected:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;

            Form2 f2 = new Form2();
            f2.ShowDialog();

            button1.Enabled = true;
        }
    }

The code for Form2:
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;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private System.ComponentModel.BackgroundWorker bgw = new BackgroundWorker();

        private void Form2_Load(object sender, EventArgs e)
        {
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        }

        private void Form2_Shown(object sender, EventArgs e)
        {
            bgw.RunWorkerAsync();
        }

        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(5000);
        }

        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}

Open in new window

0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
andre72Author Commented:
Idle Mind - for sure it works fine as the basic model ist great work done by you ;-)
And this works fine when you delete the line bgw.ReportProgress((int)(zo.FSize / cpos * 100), null);
e.g.:
if (ByteLen > 0)
{
    cpos += ByteLen;
    //bgw.ReportProgress((int)(zo.FSize / cpos * 100), null);
}

It just crashs when the ReportProgress will raise ...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm...ok.  So perhaps there is still a pending ProgressChanged() event that it tries to process after the DialogResult() has been set and the form has been hidden?

Maybe we can cheat and instead of using RunWorkerCompleted() and you can pass a sentinel value of -1 with null as UserState to ProgressChanged() and set the DialogResult() from there.

*The below is untested...

Change:

    if (ZipEntry == null) break;

To:

    if (ZipEntry == null)
    {
        bgw.ReportProgress(-1, null);
        break;
    }



New bgw_ProgressChaged():

        void bgw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            if (e.UserState == null)
            {
                if (e.PrgressPercentage == -1)
                {
                    this.DialogResult = DialogResult.Cancel;
                }
                else
                {
                    progressBar1.Value = e.ProgressPercentage;
                }
            }
            else
            {
                ZipObj zo = (ZipObj)e.UserState;
                label1.Text = zo.FName;
                progressBar1.Value = 0;
            }
        }

Comment out the RunWorkerCompleted() code:   (or just not wire it up)

        void bgw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            // DialogResult = DialogResult.Cancel;
        }
0
 
andre72Author Commented:
Unpacking had been finished when if (ZipEntry == null) break; will be called so I don't know why there should be any more call for ProgressChanged() than ...

But the workaround in your last posting doesn't solve the problem and the error will be the seem.
0
 
andre72Author Commented:
Oops ...
I set a breakpoint to if (ZipEntry == null) break; and it will only be excecuted when
bgw.ReportProgress((int)(zo.FSize / cpos * 100), null);
is comment out.

If I work with this ReportProgress() the break will never be excecuted ...
0
 
andre72Author Commented:
ProgressBar1.Value was more than maximum what cause an exception  ... Thanks for your support
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Glad you figured it out!...  =)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now