0

I'm doing a long loop checking for the content of a folder, split in three part.
p1 - This folder have more than 5000 folders in it,
p2 - which have ~10 folders each,
p3 - then an unknow number of files/folders for each.

I've made a progress bar with the maximum value = p1.nbFolder at the end of each p1.folder i increase the progressbar.Value by one. but the window freeze and the only things working to refresh the windows at the end of each folder, is a MessageBox.Show(" ");

this.UpdateLayout(); isn't working
Thread.Sleep(0); neither

for (int inc = 0; inc < postes.Items.Count; inc++) {
                poste = (TreeViewItem)postes.Items[inc];
                poste.IsSelected = true;
                tranches = (TreeViewItem)tr_tranche.Items[0];
                for (int incb = 0; incb < tranches.Items.Count; incb++) {
                    tranche = (TreeViewItem)tranches.Items[incb];
                    tranche.IsSelected = true;

                    StackPanel SPposte = (StackPanel)poste.Header;
                    Label Lposte = (Label)SPposte.Children[1];
                    String Sposte = (String)Lposte.Content;

                    StackPanel SPtranche = (StackPanel)tranche.Header;
                    Label Ltranche = (Label)SPtranche.Children[1];
                    String Stranche = (String)Ltranche.Content;

                    //str.Append("Poste : " + Sposte + " | Tranche : " + Stranche + " | indice : " + incb + " \n");

                    if (tranche != null) {
                        StackPanel stack = (StackPanel)tranche.Header;
                        Label lbl = (Label)stack.Children[1];
                        String nom = lbl.Content.ToString();

                        tr_folio.Items.Clear();
                        _folio = getDossier(nom, _tranche);
                        //ne filtre rien
                        _folio.getDossiers("a*");
                        _folio.getFichiers();
                        tr_folio.Items.Add(Utils.TreeUtils.ContenuDossier(_folio));
                        lbl_source.Content = _folio.Chemin;

                        TreeViewItem entete = (TreeViewItem)tr_folio.Items[0];
                        entete.IsExpanded = true;
                    }
                    //MessageBox.Show(incb.ToString());
                    tranche.IsSelected = false;
                }
                poste.IsSelected = false;
                pgrB.Value++;
                Thread.Sleep(1);
                this.UpdateLayout();
MickMRCX
  • 153
  • 12

3 Answers3

0

The main UI thread needs to be free to process Windows messages. If you move your work to a background thread, the UI thread will be able to paint windows, respond to user input (like cancel buttons), etc.

There are many examples of multithreading with WPF. For example, see this question for some good information.

Community
  • 1
  • 1
Paul Williams
  • 16,585
  • 5
  • 47
  • 82
0

One of the best solutions to a potentially blocking bit of code is to use a thread so that it doesnt block the UI from anything, or any other code. It runs in its own little world.

Task mytask = Task.Factory.StartNew(myfunc,aparam);

you can even do (along the lines of, not put in coding window to check syntax)

var mytask = Task.Factory.StartNew(myfunc).ContinueWith( result => its_done_func());

So you can tell when its done, you can also use a function to update with all the results. You can of course use lambda's too

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

I've found this tutorial. ProgressBar control tutorial

Paul > Your solution is almost identical, but I did not find enough explanation to make a working solution

MickMRCX
  • 153
  • 12