Solved

Selfclosing form when unpacking is done

Posted on 2010-09-07
9
282 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
  • 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 85

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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

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 85

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now