Hi all,
Can u plz provide prograssbar coding using threding.
Thanq u
Regards
Anuradha
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jebasinghPosted Jul 19, 2007, 6:10 AM
Initially the progress bar should be hidden(visible = false).
below is the code...
using
System;using
System.Drawing;using
System.Collections;using
System.ComponentModel;using
System.Windows.Forms;using
System.Data;using
System.Threading;namespace
ExtractMessageWrapper{
public delegate void DelegateThreadStart(); public delegate void DelegateThreadFinished(); public delegate void DelegateThreadMessage(int val); public class Form1 : System.Windows.Forms.Form{
private System.Windows.Forms.Button button1; private System.Windows.Forms.ProgressBar progressBar1; ///
public DelegateThreadStart m_DelegateThreadStart; public DelegateThreadFinished m_DelegateThreadFinished; public DelegateThreadMessage m_DelegateThreadMessage; public Form1(){
// // Required for Windows Form Designer support //InitializeComponent();
m_DelegateThreadStart =
new DelegateThreadStart(this.ThreadStart);m_DelegateThreadFinished =
new DelegateThreadFinished(this.ThreadFinished);m_DelegateThreadMessage =
new DelegateThreadMessage(this.ThreadIncrement);}
///{
if( disposing ){
if (components != null){
components.Dispose();
}
}
base.Dispose( disposing );}
#region
Windows Form Designer generated code ///{
this.button1 = new System.Windows.Forms.Button(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(200, 88); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(24, 24); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(352, 24); this.progressBar1.Step = 1; this.progressBar1.TabIndex = 1; this.progressBar1.Visible = false; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(400, 182); this.Controls.Add(this.progressBar1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);}
#endregion
///[STAThread]
static void Main(){
Application.Run(
new Form1());}
Thread WorkerThread;
private void button1_Click(object sender, System.EventArgs e){
WorkerThread =
new Thread(new ThreadStart(this.WorkerThreadFunction));WorkerThread.Name = "Progress Bar";
WorkerThread.Start();
}
private void WorkerThreadFunction(){
Invoke(m_DelegateThreadStart,
null); for(int i = 0; i< 100; i++){
Invoke(m_DelegateThreadMessage,
new object[]{i}); // here you can perform some other action .. // i use thread sleep to show you how the thread is workingThread.Sleep(500);
}
Invoke(m_DelegateThreadFinished,
null);}
private void ThreadStart(){
progressBar1.Visible =
true;progressBar1.Value = 0;
}
private void ThreadFinished(){
progressBar1.Visible =
false;}
private void ThreadIncrement(int val){
if (progressBar1.Value <= 99){
progressBar1.Value = val ;
}
}
}
}
public DelegateThreadFinished m_DelegateThreadFinished; public DelegateThreadMessage m_DelegateThreadMessage;let me explain what i have done in above code
i have written three functions they are
ThreadStart() - which actualy makes progressbar visible
Threadfinished() - hides the progressbar
ThreadIncrement(int val) - increments the value.
i have delcared three delegates also which refer these functions
public DelegateThreadStart m_DelegateThreadStart;
Actual mapping happens for delegate and functions
m_DelegateThreadStart = new DelegateThreadStart(this.ThreadStart);
m_DelegateThreadFinished =
new DelegateThreadFinished(this.ThreadFinished);m_DelegateThreadMessage =
new DelegateThreadMessage(this.ThreadIncrement);declare a thread
Thread WorkerThread;
and start the thread like this
WorkerThread = new Thread(new ThreadStart(this.WorkerThreadFunction));
WorkerThread.Name = "Progress Bar";
WorkerThread.Start();
best of luck.....