Solved

Populating TreeView Control (form1) & ProgressBar (form2)

Posted on 2008-10-03
10
2,012 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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