Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selfclosing form when unpacking is done

Posted on 2010-09-07
9
Medium Priority
?
292 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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