Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20
  1. #11
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Since i am still a bit stumped on this i have decided to clean up the code a little, and post the relevant pieces here in hopes that somebody can help come up with a way to get the darn progress bar to step for each file thats packed

    Here is the pack.cs
    Code:
        static class Pack
        {
            public static void zip(string targetFilename, List<string> fileList, string rootDir)
            {
                ZipFile zf = ZipFile.Create(targetFilename);
    
                ((ZipNameTransform)zf.NameTransform).TrimPrefix = rootDir;
    
                zf.BeginUpdate();            
    
                foreach (string i in fileList)
                {
                    Console.WriteLine("before adding: " + i);
                    zf.Add(i);
                }
    
                Console.WriteLine("commit update");
                zf.CommitUpdate();
                Console.WriteLine("close");
                zf.Close();
            }
        }
    The interesting part is of course the part with the foreach loop sectioned out here:
    Code:
                foreach (string i in fileList)
                {
                    Console.WriteLine("before adding: " + i);
                    zf.Add(i);
                }

    The graphical form part is here with the irelevant sections cut out:

    Form1.cs
    Code:
    ...
            private void buttonPack_Click(object sender, EventArgs e)
            {
                try
                {
                    buttonPack.Text = "Packed";
                    buttonPack.Enabled = true;
    
                    List<string> filesToPack = new List<string>();
                    filesToPack = ProjectHandling.Solution(inputFilename.Text);
                    string test = Path.GetDirectoryName(inputFilename.Text);
                    Pack.zip(targetDir.Text, filesToPack, Path.GetDirectoryName(inputFilename.Text));
    
                    labelMessage.Text = "Done!";
    
                    messageList.Items.Add("packing is complete!");
                }
                catch (Exception exc)
                {
                    AddToMessage(exc.Message.ToString());
                }
            }
    
    ...
    I cut out all the other peices because its when the button is pressed that the packing process starts. More precisely its on the
    Code:
    Pack.zip(targetDir.Text, filesToPack, Path.GetDirectoryName(inputFilename.Text));
    That we pack the files, and the loop inside there that should be updating meanwhile.

    Hope this helps somebody to help me
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  2. #12
    Join Date
    Jun 2003
    Posts
    434
    here try using this method
    click


    to make things a bit easier I would have the thing run in states example: gatheringFiles, packing, completed etc..

    gathering files you could have the progress bar do "ProgressBarStyle.Marquee" only necessary if there are alot of files.


    `Baker
    Last edited by BakerCo; 11-03-2008 at 03:17 PM.
    My 3DS Max Sprite Sheet Renderer
    A government big enough to give you everything you want,
    is strong enough to take everything you have.
    -- Thomas Jefferson

  3. #13
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Quote Originally Posted by Swoop View Post
    [code]
    zf.BeginUpdate();

    foreach (string i in fileList)
    {
    Console.WriteLine("before adding: " + i);
    zf.Add(i);
    }

    Console.WriteLine("commit update");
    zf.CommitUpdate();
    Of course you could begin and end update with each file and increment the progress bar.. Which would be more representtive of the whole how far you got.. and I guess technically would mean some files got added and you could do a try/catch to handle any which didnt.

    btw - where did you get the zipfile class from?
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  4. #14
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Got it from a friend who did the original idea for the project packer. But he made a console version that we thought was too troublesome to use rather than a simple gui

    My coding-buddy (hehe) is comming over tonight so we should be able to finish it there.. either by moving the loop into Form1.cs or by adding events. Just seems like delegates and events are the best solution also education wise
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  5. #15
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Would be interested to know if you could share the zip code - as it looks like a nice and simple to use class.. Always good to have simple stuff to use
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  6. #16
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Of course i can share it.. and i can do it more easily after tonight were we hopefulle get the progress bar working so we can call it done for this version of the program

    I will post a link to our messy code tonight after we finish

    Again if you, or anybody else, knows how and where to use delegates to get our code working easier please let me know
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  7. #17
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Sure, theres actually plenty on MSDN about delegates.

    example:

    Lets decide we need to know that a value is changing in a property (close to home but not quite)

    We have a class

    it currently looks like: (ignore variable notation.. its quick to use 1 letter!)
    Code:
        public class myclass
        {
            private int a;
            public int A
            {
                get { return a; }
                set { a = value; }
            }
        }
    
    //Which say in your form load you added
    
                myclass mc = new myclass();
                mc.A = 1;
    You now have mc.A = 1.. but well who knew..

    So, now we want to know if/when it changes.

    So

    Code:
    // need a line for the deligate description
    
        public delegate void OnChangehandle(int oldvalue, int newvalue);
    
    // the class now having an event to assign a delegate to, and a way to call it..
    
        public class myclass
        {
            private int a;
            public int A
            {
                get { return a; }
                set { 
                     Changed(a,value);
                     a = value; }
            }
    
            public event OnChangehandle OnChange;
    
            protected virtual void Changed(int oldvalue, int newvalue)
            {
                if (OnChange != null)
                    OnChange(oldvalue, newvalue);
            }
    
        }
    
    // we need something to run when our event triggers, so..
    
            void itemchanged(int oldv, int newv)
            {
                Text = String.Format("Old: {0} New: {1}", oldv, newv);
            }
    
    // and our form load code could now look like
    
            private void Form1_Load(object sender, EventArgs e)
            {
                myclass mc = new myclass();
                mc.OnChange += new OnChangehandle(itemchanged);
                mc.A = 1;
            }
    Welcome to events and delegates
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  8. #18
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Thanks for the help Liz. With your code example my friend actually got it working.. although i havent had time to clean up the code, nor dissect what he did yet.

    As promised here is the source for APP (awesome project packer) hehe and included the zip funtion requested

    Hope you will enjoy, and feel free to point out bugs in the VERY messy code. We havent found time to clean up the code nor rename stuff yet. Secondary to getting our stuff working so we can use it hehe

    Awesome Project Packer (APP) Source Code Download
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  9. #19
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Thanks I'll take a look once I've been to physio and done the minim of work to apease the masses
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  10. #20
    Join Date
    Dec 2008
    Posts
    1
    Bear with me as all the coding below is done in the browser;

    Code:
        /// <summary>
        /// Handles the events and invocation for our ProgressBar incrementation.
        /// </summary>
        public delegate void ProgressIncrementHandler();
        class Pack
        {
            /// <summary>
            /// Occurs when <see cref="Zip"/> zips a new file.
            /// </summary>
            /// 12/19/2008 1:55 PM
            public static event ProgressIncrementHandler OnProgressIncrement;
    
            /// <summary>
            /// Invokes the OnProgressIncrement event.
            /// </summary>
            private static void InvokeOnProgressIncrement()
            {
                // Make sure we have something hooked to the event before
                // calling it.
                ProgressIncrementHandler Handler = OnProgressIncrement;
                if (Handler != null)
                {
                    Handler();
                }
            }
    
            public static void Zip(string targetFilename, IEnumerable<string> fileList, string rootDir)
            {
                ZipFile zf = ZipFile.Create(targetFilename);
    
                ((ZipNameTransform)zf.NameTransform).TrimPrefix = rootDir;
    
                zf.BeginUpdate();            
    
                foreach (string i in fileList)
                {
                    zf.Add(i);
    
                    // Invoke our progress bar event.
                    // This will tell our main form to update the current
                    // value on the progress bar!
                    InvokeOnProgressIncrement();
                }
    
                Console.WriteLine("commit update");
                zf.CommitUpdate();
                Console.WriteLine("close");
                zf.Close();
            }
        }
    
        public partial class Form1: Form
        {
            public Form1()
            {
                InitializeComponent();
                // Hook our event.
                Pack.OnProgressIncrement += new ProgressIncrementHandler(Pack_OnProgressIncrement);
            }
    
            void Pack_OnProgressIncrement()
            {
                // If we're calling this method from another thread
                // we need to invoke on the proper thread to avoid
                // having IllegalCrossThreadCall exceptions being thrown.
                if (InvokeRequired)
                    Invoke(new ProgressIncrementHandler(Pack_OnProgressIncrement));
                else
                {
                    // Don't increment if we're at the progbar.Maximum
                    // This will cause exceptions.
                    if (myProgressBar.Value < myProgressBar.Maximum)
                        myProgressBar.Increment(1);
                }
            }
    
            private void buttonPack_Click(object sender, EventArgs e)
            {
                try
                {
                    buttonPack.Text = "Packed";
                    buttonPack.Enabled = true;
    
                    List<string> filesToPack = new List<string>();
                    filesToPack = ProjectHandling.Solution(inputFilename.Text);
    
                    // Setup the progress bar (Assuming you have named it to this
                    // on the main form, change if necessary)
                    myProgressBar.Minimum = 0;
                    myProgressBar.Maximum = filesToPack.Count;
                    myProgressBar.Value = 0;
                    // You may also want to set myProgressBar.Visible = true
                    // if the default is false.
    
                    string test = Path.GetDirectoryName(inputFilename.Text);
    
                    // While Pack.zip is running, it will constantly fire an event
                    // that will be caught by Pack_OnProgressIncrement()
                    // to update the progress bar.
                    Pack.zip(targetDir.Text, filesToPack, Path.GetDirectoryName(inputFilename.Text));
    
                    // Clean up our Progress bar
                    myProgressbar.Maximum = 100;
                    myProgressbar.Value = 0;
                    // Optionally set the .Visible = false
    
                    labelMessage.Text = "Done!";
    
                    messageList.Items.Add("packing is complete!");
                }
                catch (Exception exc)
                {
                    AddToMessage(exc.Message.ToString());
                }
            }
        }
    Again, if there's some minor things wrong, please forgive me.

    Hope that helps.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •