Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 22-May-11 21:24pm
v3
Comments
Sergey Alexandrovich Kryukov 23-May-11 4:13am    
Look at your tags! "progress", "bar"... Is is WPF, Forms, what?!
--SA

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
 
Share this answer
 
Comments
CS2011 23-May-11 4:43am    
Even i was wondring why he using sleep here.Make no sense. My 5 for your answer.
Sergey Alexandrovich Kryukov 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 23-May-11 5:28am    
My 5 for "Never ever sleep" :-)
Good answer to.
Sergey Alexandrovich Kryukov 23-May-11 9:30am    
Thank you, Kim.
--SA
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,
 
Share this answer
 
Comments
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?
Thanks
Sergey Alexandrovich Kryukov 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
Sergey Alexandrovich Kryukov 23-May-11 4:20am    
See also my solution.
--SA
andywawa 24-May-11 2:55am    
Hi,
can you help any further? I'd stucked..
Best regards
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900