Solved

Populating TreeView Control (form1) & ProgressBar (form2)

Posted on 2008-10-03
10
1,971 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
  • 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 400 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 400 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 400 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now