Click here to Skip to main content
13,196,730 members (57,370 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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;
                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);

                myAl = null;
                helpArr= null;
            catch (Exception ex)
                MessageBox.Show("Some Problems with copy." + ex.Message, "Error.", MessageBoxButtons.OK,
                result = false;
            return result;

What I'm doing wrong?
Thanks for your help

[edit]"Ignore HTML..." option disabled - OriginalGriff [/edit]
Posted 22-May-11 21:22pm
Updated 22-May-11 21:24pm
SAKryukov 23-May-11 4:13am
Look at your tags! "progress", "bar"... Is is WPF, Forms, what?!
Rate this: bad
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[^],
Control events not firing after enable disable + multithreading[^].

CS2011 23-May-11 4:43am
Even i was wondring why he using sleep here.Make no sense. My 5 for your answer.
Kim Togo 23-May-11 5:28am
My 5 for "Never ever sleep" :-)
Good answer to.
SAKryukov 23-May-11 9:29am
You were right. If it's harmful because sleep time is short, then it makes no sense.
Thank you.
SAKryukov 23-May-11 9:30am
Thank you, Kim.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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,
andywawa 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?
SAKryukov 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!
SAKryukov 23-May-11 4:20am
See also my solution.
andywawa 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.
andywawa 24-May-11 2:55am
can you help any further? I'd stucked..
Best regards

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171019.1 | Last Updated 23 May 2011
Copyright © CodeProject, 1999-2017
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