?
Solved

Dynamic Progress Bar

Posted on 2005-04-25
10
Medium Priority
?
1,787 Views
Last Modified: 2012-05-05
I have already asked this question once but haven't got a proper answer.

I have two forms, main form and some sub form. There is a function in the main form that does some calculations and performs some operations which may take between 5 to 10 seconds (involving database access and file transferring).

This function also initializes the second form, passing results of all operations to it and displaying on the top of the main form.

During these 5-10 seconds my first form is getting frozen and I wold like to show a progress bar on a separate form probably, showing the progress untill all operations are done. Presence of cancel button is optional but preferable.

I understand that I have to start my operation in a separate thread. I found some links but it's not still very much clear to me.

I would appreciate your help but coding examples would be more appreciated than links to other 50-topic messages ;)

That's all for windows-forms using c#

regards

0
Comment
Question by:Yurich
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 400 total points
ID: 13863886
I gave an example as well as a discussion of various methods in vb.net here http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21054625.html ... if you need it in C# let me know but it is only a few lines of code and the premise is the same.

Greg
0
 
LVL 21

Author Comment

by:Yurich
ID: 13864036
OK, if there is no way to find the exact time it's fine.
One of the ways beside of gif animation and cycling around would be stopping on 99% and waiting until it's done, specially that I've got at least some approximation. The gif animation can be fine  - that would be the same as Windows uses for file copying and etc but it will envole getting these gifs somwhere...

And the question is still open. How can I display an interim form with a progress bar (not dynamic anymore) while some operations are getting done and terminate it when the second form is about to be displayed.

Again I understand it should be all done in separate threads but my expertise of threads is very limited, thus I'd appreciate a code snipped or working sample.

Regards,


0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13864155
will do in the morning. 12am here and time for bed, should only take a minute or two.

as for the animated gif you wouls just show the form ... then do the operation the close the form ...

for threaded operations you would raise events for updates and for the end which would close the form ...

Greg
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

by:rashmitodkar
ID: 13865132
0
 
LVL 21

Author Comment

by:Yurich
ID: 13865227
thanks rashmitodkar,
but as i mentioned in my original message that's for windows forms
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 13865248
no thatz for web app and i used that in web app
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 13865255
oh sorry i thought its for web app.
anyways i think there must be some progressbar availabe in winforms.
0
 
LVL 21

Author Comment

by:Yurich
ID: 13865265
that's what I'm after...
0
 
LVL 8

Accepted Solution

by:
vinhnl earned 1600 total points
ID: 13865369
See my sample:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DemoProgress
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class MainForm : System.Windows.Forms.Form
      {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            private System.Windows.Forms.Button btnDoJobs;
            private SubForm form = null;

            public MainForm()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.btnDoJobs = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // btnDoJobs
                  //
                  this.btnDoJobs.Location = new System.Drawing.Point(112, 120);
                  this.btnDoJobs.Name = "btnDoJobs";
                  this.btnDoJobs.TabIndex = 0;
                  this.btnDoJobs.Text = "Do jobs";
                  this.btnDoJobs.Click += new System.EventHandler(this.btnDoJobs_Click);
                  //
                  // MainForm
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 189);
                  this.Controls.Add(this.btnDoJobs);
                  this.Name = "MainForm";
                  this.Text = "Main Form";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new MainForm());
            }

            private void MyJob()
            {
                  for(long i = 0;i<100000000;i++)
                  {
                        if(i % 100000 == 0)
                              form.ShowJobs(i.ToString());
                  }
            }

            private bool Stop = false;
            private void form_CancelJobs(object sender, System.EventArgs e)
            {
                  Stop = true;
            }

            private void btnDoJobs_Click(object sender, System.EventArgs e)
            {
                  Stop  = false;
                  form = new SubForm();
                  form.CancelJobs +=new EventHandler(form_CancelJobs);
                  form.Show();
                  System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ThreadStart( MyJob ) );
                  thread.Priority = System.Threading.ThreadPriority.Lowest;
                  thread.Start();
                  while(!thread.Join(10) && !Stop)
                        Application.DoEvents();
                  if(Stop && !thread.Join(10))
                        thread.Interrupt();
                  form.Close();
            }
      }
}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DemoProgress
{
      /// <summary>
      /// Summary description for Form2.
      /// </summary>
      public class SubForm : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ProgressBar progressBar1;
            private System.Windows.Forms.Timer timer1;
            private System.Windows.Forms.Button btnCancel;
            private System.Windows.Forms.Label lblResult;
            private System.ComponentModel.IContainer components;

            public SubForm()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.btnCancel = new System.Windows.Forms.Button();
                  this.progressBar1 = new System.Windows.Forms.ProgressBar();
                  this.timer1 = new System.Windows.Forms.Timer(this.components);
                  this.lblResult = new System.Windows.Forms.Label();
                  this.SuspendLayout();
                  //
                  // btnCancel
                  //
                  this.btnCancel.Location = new System.Drawing.Point(112, 80);
                  this.btnCancel.Name = "btnCancel";
                  this.btnCancel.TabIndex = 1;
                  this.btnCancel.Text = "Cancel";
                  this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
                  //
                  // progressBar1
                  //
                  this.progressBar1.Location = new System.Drawing.Point(40, 48);
                  this.progressBar1.Name = "progressBar1";
                  this.progressBar1.Size = new System.Drawing.Size(224, 16);
                  this.progressBar1.TabIndex = 2;
                  //
                  // timer1
                  //
                  this.timer1.Enabled = true;
                  this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                  //
                  // lblResult
                  //
                  this.lblResult.Location = new System.Drawing.Point(40, 8);
                  this.lblResult.Name = "lblResult";
                  this.lblResult.Size = new System.Drawing.Size(224, 23);
                  this.lblResult.TabIndex = 3;
                  //
                  // SubForm
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 125);
                  this.Controls.Add(this.lblResult);
                  this.Controls.Add(this.progressBar1);
                  this.Controls.Add(this.btnCancel);
                  this.Name = "SubForm";
                  this.Text = "Sub Form";
                  this.Load += new System.EventHandler(this.Form2_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            private void Form2_Load(object sender, System.EventArgs e)
            {
            
            }

            private void timer1_Tick(object sender, System.EventArgs e)
            {
                  if(progressBar1.Value == 100)
                        progressBar1.Value = 0;
                  else
                        progressBar1.Value ++;
            }

            public event EventHandler CancelJobs;

            private void btnCancel_Click(object sender, System.EventArgs e)
            {
                  if(CancelJobs != null)
                        CancelJobs(this,e);
            }

            public void ShowJobs(string result)
            {
                  lblResult.Text = result;
            }
      }
}

Good luck
VINHNL
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 13865462
hi
 try this
 http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
 i think you can use this splash screen which runs in different thread.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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