Solved

Populating TreeView Control (form1) & ProgressBar (form2)

Posted on 2008-10-03
10
1,952 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

16 Experts available now in Live!

Get 1:1 Help Now