Solved

Selfclosing form when unpacking is done

Posted on 2010-09-07
9
288 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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