?
Solved

Populating TreeView Control (form1) & ProgressBar (form2)

Posted on 2008-10-03
10
Medium Priority
?
2,022 Views
Last Modified: 2013-12-17
I am porting over an Outlook add-in from Visual Basic 6 to C# 2.0 (Visual Studio 2008).  I have two forms a "main" and  "progress" in which the main has all the logic and controls while progress just contains a progressbar control.

The application makes several calls to various databases on startup so I would like to display an "activity/indeterminate" progress indicator while the main form is processing.  My question is how can I display this progressbar?  Using Google I have seen mention of threads and delegates but I have not made many successful inroads in this arena.  

The code below does not work because the Progressbar does indeed show but never repaints.   Or depending upon order the progressbar form might show up but not until after the main form load is complete thus defeating the purpose.

Ideally I would like three separate threads main (default), BuildMyTree (populates treeview control), and Progress (updates progressbar).  But being unfamiliar with the techniques and running to controls that are not thread-safe I am uncertain of how to proceed.
//pseudo
 
Progress pb = new Progress();  //ProgressBar form
pb.Show();
 BuildMyTree(); //Main form gathers data and fills tree
pb.Close();

Open in new window

0
Comment
Question by:CyberUnDead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 22639078
Just showing the form containing the progressbar is not enough.
Either you have to periodically set the position of the progress bar to indicate the progress or else you can set the Style property of the progress bar to Marquee. With the latter option you wont have to set the position of the progress bar.
0
 

Author Comment

by:CyberUnDead
ID: 22649478
CuteBug:

  Thanks for the reply however the progressbar style is set to Marquee.  From what I have gathered from Google a resource intensive process will "freeze" the GUI and not allow it properly update.  My assumption is that is the case as again the progressbar shows but does not contain any movement.
0
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 1600 total points
ID: 22650415
Hmmm...
Updating the progress bar in a separate thread will solve the issue.

The following links will provide some insight
http://bytes.com/forum/thread566571.html
http://www.dreamincode.net/forums/blog/martyr2/index.php?showentry=680
0
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 

Author Comment

by:CyberUnDead
ID: 22651648
CuteBug:

I have tried to adapt the code in the first link you provided but I have the same result.  The progressbar shows but does not start moving until after the main form has loaded.

The entry function of the delegate is located on the second form "progress"

      public void EnableContinuous()
        {
            progressBar1.Style = ProgressBarStyle.Marquee;
            progressBar1.MarqueeAnimationSpeed = 30;
        }

But the rest of the code is in the main form.
//--BEGIN
//  In my main section of the 'main form'
            IntPtr handle = pb.Handle; // Hack: Force form handle to update
            pb.BeginInvoke(new ChangeProgressBarCallback(pb.EnableContinuous));
            pb.Show();
            BuildTree();
            pb.BeginInvoke(new ChangeProgressBarCallback(pb.DelayClose));
//---END
 
 
//--BEGIN
//  In the main form
 
  // declare delegate signature
        private delegate void ChangeProgressBarCallback();
 
        private void changeProgressBar()
        {
            if (pb.InvokeRequired)
            {
                // instantiate the callback instance out of this very method
                ChangeProgressBarCallback callback = new ChangeProgressBarCallback(changeProgressBar);
 
                // invoke it, when it comes to it again InvokeRequired will be false
                Invoke(callback);
            }
            else
            {
                //?
                pb.Show();
            }
        }
//---END

Open in new window

0
 
LVL 16

Accepted Solution

by:
CuteBug earned 1600 total points
ID: 22651982
Since your BuildTree is taking a lot of CPU time, the best thing would be to run it asynchronously.
Use a BackgroundWorker for that.
http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx

If you do this, you dont need to call the Invoke method of the Progress bar.

Do the following


//--BEGIN
//  In my main section of the 'main form'
            pb.Style = ProgressBarStyle.Marquee;
            pb.MarqueeAnimationSpeed = 30;
 
        private BackgroundWorker worker = null;
        worker = new BackgroundWorker();
        worker.DoWork +=new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerAsync();
//---END
 
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BuildTree();
        }

Open in new window

0
 

Author Comment

by:CyberUnDead
ID: 22652337
CuteBug:

  I have your background worker code implemented but I am running to a marshalling error for threads.  The BuildTree() method populates a TreeView control but my understanding is controls are not thread safe from my initial Googling.  The exception I receive is:

Action being performed on this control is being called from the wrong thread. Marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 22652406
Check this link to know how to populate TreeView Control on a separate thread.
0
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 1600 total points
ID: 22652409
0
 

Author Comment

by:CyberUnDead
ID: 22652479
CuteBug:

  Thanks for all the information.  I will need to take some time to grok all of this.
0
 

Author Closing Comment

by:CyberUnDead
ID: 31502939
Thanks I was able to put it all together to get a working solution.
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question