Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C# progress bar
Hello,
on the main form I start a routine that begins with copying some files (about 1,6GB).
It takes a while so I want to show the progress of the copying function using a progress bar. But the form "freezes" and the progress bar doesn't show up at all.
The code:
private bool CopyingWithProgressBar(ArrayList myAl)
        {
            bool result = true;
            try
            {
                Object[] helpArr = myAl.ToArray();
                pBar1.Visible = true;
                pBar1.Minimum = 0;
                pBar1.Maximum = helpArr.Length;
                pBar1.Value = 1;
                pBar1.Step = 1;
 
                for (int x = 0; x < helpArr.Length; x++)
                {
                    string input = helpArr[x].ToString().Substring(0, helpArr [x].ToString().IndexOf(","));
                    string target = helpArr[x].ToString().Substring(helpArr [x].ToString().IndexOf(",") + 1);
                    File.Copy(input, target, true);
                    Thread.Sleep(100);
                    pBar1.PerformStep();
 

                }
                myAl.Clear();
                myAl.TrimToSize();
                myAl = null;
                helpArr= null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Some Problems with copy." + ex.Message, "Error.", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                result = false;
            }
            return result;
        }
What I'm doing wrong?
Thanks for your help
 
[edit]"Ignore HTML..." option disabled - OriginalGriff [/edit]
Posted 22-May-11 22:22pm
Edited 22-May-11 22:24pm
v3
Comments
SAKryukov at 23-May-11 4:13am
   
Look at your tags! "progress", "bar"... Is is WPF, Forms, what?!
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Never ever sleep or call any blocking methods on UI threads.
For write techniques, please see a collection of links to my past answers on the problem:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].
 
—SA
  Permalink  
Comments
CS2011 at 23-May-11 4:43am
   
Even i was wondring why he using sleep here.Make no sense. My 5 for your answer.
SAKryukov at 23-May-11 9:29am
   
You were right. If it's harmful because sleep time is short, then it makes no sense.
Thank you.
--SA
Kim Togo at 23-May-11 5:28am
   
My 5 for "Never ever sleep" :-)
Good answer to.
SAKryukov at 23-May-11 9:30am
   
Thank you, Kim.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
In a nutshell, the progress bar runs on the same thread as the main form. Which will render the progress bar in a non responsive state, until the work on the main thread has finished. Please have a look at the following links:
 
Multiple Thread Progress Bar Control[^]
c# winform background worker and progress bar[^]
 
Kind regards,
  Permalink  
Comments
andywawa at 23-May-11 4:03am
   
Thanks for your answer. Hm, there is another problem with background worker.
1. The worker doesn't "wait" and comes immidiately back to method that calls him. Acutally it's essential to me, that all the methods are running top-down (first: coopying, then installing, otherwise it makes no sense..) I know, I could put the next method calls in the worker method "on complition", but it means I'd have to change o lot in my code. Is there any other method to run progress bar without using the background worker?
Thanks
SAKryukov at 23-May-11 4:16am
   
Don't even play with the idea of not using thread; it would kill the whole your project. If not background worker than a regular thread, either Thread constrictor or thread pool. Nothing else!
--SA
SAKryukov at 23-May-11 4:20am
   
See also my solution.
--SA
andywawa at 24-May-11 2:55am
   
Hi,
can you help any further? I'd stucked..
Best regards
andywawa at 23-May-11 5:12am
   
Hi, thanks for your answer and working example. That explains a lot (I'm still reading your code, some things are new to me..). Anyway: I still don't know how to put other methods together and make them work top-down? My goal: every single step should wait for complition of the previous one (copying, installing, executing some scripts, copying, install other routine etc.). At every single step I want to show the progress bar stating the app-progress. How to make the single-methods wait "for each other"? As I said: it's essential that one method waits for another one to be finished for there is no point trying to ínstall before some files needed for the installation are not copied yet etc. How to put the single steps together ( a Queue or something)? Thanks for any hints.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 23 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100