Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populating TreeView Control (form1) & ProgressBar (form2)

Posted on 2008-10-03
10
Medium Priority
?
2,048 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 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