Solved

Selfclosing form when unpacking is done

Posted on 2010-09-07
9
286 Views
Last Modified: 2013-12-17
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
Comment
Question by:andre72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 33620372
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
 

Author Comment

by:andre72
ID: 33620437
No the extracting of the archive is complete than ...
It's an TargetInvocationException I get.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33622516
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:andre72
ID: 33622573
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33622647
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
 

Author Comment

by:andre72
ID: 33625179
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
 

Author Comment

by:andre72
ID: 33625239
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
 

Author Closing Comment

by:andre72
ID: 33625505
ProgressBar1.Value was more than maximum what cause an exception  ... Thanks for your support
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33626460
Glad you figured it out!...  =)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question