473,700 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't update foreground panel from background thread

I'm trying to figure out how to modify a panel (panel1) from a
backgroundworke r thread. But can't get the panel to show the new controls
added by the backgroundwork task. Here is my code. In this code there is a
panel panel1, that I populate with a lable in the foreground. Then when I
click on "button1" a backgroundworke r thread in async mode is started. When
the backgoundworker thread completes the thread returns a panel to populate
the panel1 control in the form with two controls label1 and lable 2. What I
can't figure out is how to redraw the form, or panel so the two new controls
are displayed. Does anyone know how to change my code to resolve this
problem?

Here is my code:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Label label1 = new Label();
label1.Text = "Label 1 text from foreground thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
Panel panelTemp = new Panel();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
e.Result = panelTemp;

}

private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}
Dec 19 '06 #1
8 5363
Hi Greg,
you cannot update a control from a thread that did not create the control.
In your case you are adding the labels to the panel from a different thread
so that is probably why it is not updating automatically. What you want to
do is transfer control back to the main UI thread to update the panel, you
can do this using Invoke, so inside of backgroundWorke r1_DoWork, do something
like:

void private void backgroundWorke r1_DoWork(..... )
{
//Update panel
UpdatePanel();
}

private void UpdatePanel()
{
if(this.panelTe mp.InvokeRequir ed)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp. Invoke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
}
}

Mark
--
http://www.markdawson.org
"Code like the person who maintains your code is a axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
I'm trying to figure out how to modify a panel (panel1) from a
backgroundworke r thread. But can't get the panel to show the new controls
added by the backgroundwork task. Here is my code. In this code there is a
panel panel1, that I populate with a lable in the foreground. Then when I
click on "button1" a backgroundworke r thread in async mode is started. When
the backgoundworker thread completes the thread returns a panel to populate
the panel1 control in the form with two controls label1 and lable 2. What I
can't figure out is how to redraw the form, or panel so the two new controls
are displayed. Does anyone know how to change my code to resolve this
problem?

Here is my code:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Label label1 = new Label();
label1.Text = "Label 1 text from foreground thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
Panel panelTemp = new Panel();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
e.Result = panelTemp;

}

private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}

Dec 19 '06 #2
Mark thanks for the help. But I couldn't get what you said to work. My
problem is when I added the UpdatePanel() method to may Form1 Class it
couldn't find "this.panelTemp ". I assume this is because it was defined in
the backgroundWorke r1_DoWork method. Also I will be populating a the
panelTemp control in the background thread, and then I want to pass it back
to the UI thread and update the panel1 control. The UI thread will never
update the panel1 control directly. If you would provide me a working
example where I could add those two label control from to the UI panel1
control from the backgroundWorke r1_DoWork background thread that would be
great.

"Mark R. Dawson" wrote:
Hi Greg,
you cannot update a control from a thread that did not create the control.
In your case you are adding the labels to the panel from a different thread
so that is probably why it is not updating automatically. What you want to
do is transfer control back to the main UI thread to update the panel, you
can do this using Invoke, so inside of backgroundWorke r1_DoWork, do something
like:

void private void backgroundWorke r1_DoWork(..... )
{
//Update panel
UpdatePanel();
}

private void UpdatePanel()
{
if(this.panelTe mp.InvokeRequir ed)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp. Invoke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
}
}

Mark
--
http://www.markdawson.org
"Code like the person who maintains your code is a axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
I'm trying to figure out how to modify a panel (panel1) from a
backgroundworke r thread. But can't get the panel to show the new controls
added by the backgroundwork task. Here is my code. In this code there is a
panel panel1, that I populate with a lable in the foreground. Then when I
click on "button1" a backgroundworke r thread in async mode is started. When
the backgoundworker thread completes the thread returns a panel to populate
the panel1 control in the form with two controls label1 and lable 2. What I
can't figure out is how to redraw the form, or panel so the two new controls
are displayed. Does anyone know how to change my code to resolve this
problem?

Here is my code:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Label label1 = new Label();
label1.Text = "Label 1 text from foreground thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
Panel panelTemp = new Panel();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
e.Result = panelTemp;

}

private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}
Dec 19 '06 #3
Hi Greg,
your code was creating the tempPanel but was never adding it to any
control collection so it was never being shown. What you want to do is only
modify the UI controls you create in the main UI thread, you can do this by
calling Invoke, below is your code modified to work and will show the two
labels:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;

namespace CSharpVision
{
public partial class VideoCanvas : UserControl
{
/// <summary>
/// The camera that the video canvas is displaying
/// </summary>
private Camera camera;

/// <summary>
/// The image currently being displayed on the video canvas.
/// </summary>
private Image currentImage;

/// <summary>
/// Used to lock update access to the image which is currently
/// to be displayed on the form.
/// </summary>
private readonly object imageLock = new object();
public VideoCanvas(Cam era camera)
{
InitializeCompo nent();
this.camera = camera;
}

/// <summary>
/// Gets the camera instance the VideoCanvas is displaying.
/// </summary>
public Camera Camera
{
get
{
return this.camera;
}
}

//TODO:CurrentIma ge should only be available on
//premium builds of the product

/// <summary>
/// Gets the image currently showing in the viewer.
/// </summary>
public Image CurrentImage
{
get
{
throw new NotImplementedE xception("Only available in the " +
"premium edition of the viewer");
}
}

protected override void OnPaintBackgrou nd(PaintEventAr gs e)
{
//Override this and do nothing so we don't get any flickering
if (this.currentIm age == null)
{
base.OnPaintBac kground(e);
}
}

protected override void OnPaint(PaintEv entArgs e)
{

//TODO: Need to make sure we lock when we update the image
lock (this.imageLock )
{
if (this.currentIm age != null)
{
//we have an image to draw

//Get the size of the image we are going to display
Rectangle sourceRegion = new Rectangle(
0, 0,
this.currentIma ge.Width,
this.currentIma ge.Height);

//Get the size of this control, this is the size we need
//to scale the image to.
Rectangle destinationRegi on = new Rectangle(
0, 0,
this.Width,
this.Height);

//Draw the image, scaling as necessary to fit the
//whole control.
e.Graphics.Draw Image(this.curr entImage,
destinationRegi on,
sourceRegion,
GraphicsUnit.Pi xel);
}
else
{
base.OnPaint(e) ;
}
}
}
}
}
Hope that helps
Mark.
--
http://www.markdawson.org
"Code like the person who maintains your code is a psychotic axe murdered -
WHO KNOWS WHERE YOU LIVE"
"Greg Larsen" wrote:
Mark thanks for the help. But I couldn't get what you said to work. My
problem is when I added the UpdatePanel() method to may Form1 Class it
couldn't find "this.panelTemp ". I assume this is because it was defined in
the backgroundWorke r1_DoWork method. Also I will be populating a the
panelTemp control in the background thread, and then I want to pass it back
to the UI thread and update the panel1 control. The UI thread will never
update the panel1 control directly. If you would provide me a working
example where I could add those two label control from to the UI panel1
control from the backgroundWorke r1_DoWork background thread that would be
great.

"Mark R. Dawson" wrote:
Hi Greg,
you cannot update a control from a thread that did not create the control.
In your case you are adding the labels to the panel from a different thread
so that is probably why it is not updating automatically. What you want to
do is transfer control back to the main UI thread to update the panel, you
can do this using Invoke, so inside of backgroundWorke r1_DoWork, do something
like:

void private void backgroundWorke r1_DoWork(..... )
{
//Update panel
UpdatePanel();
}

private void UpdatePanel()
{
if(this.panelTe mp.InvokeRequir ed)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp. Invoke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;

Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
}
}

Mark
--
http://www.markdawson.org
"Code like the person who maintains your code is a axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
I'm trying to figure out how to modify a panel (panel1) from a
backgroundworke r thread. But can't get the panel to show the new controls
added by the backgroundwork task. Here is my code. In this code there is a
panel panel1, that I populate with a lable in the foreground. Then when I
click on "button1" a backgroundworke r thread in async mode is started. When
the backgoundworker thread completes the thread returns a panel to populate
the panel1 control in the form with two controls label1 and lable 2. What I
can't figure out is how to redraw the form, or panel so the two new controls
are displayed. Does anyone know how to change my code to resolve this
problem?
>
Here is my code:
>
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
>
namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Label label1 = new Label();
label1.Text = "Label 1 text from foreground thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panel1.Controls .Add(label1);
>
}
>
private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
Panel panelTemp = new Panel();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread
>
Label label1 = new Label();
label1.Text = "Label 1 text from background thread";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label1) ;
>
Label label2 = new Label();
label2.Text = "Label 2 text from background thread";
label2.Location = new Point(1, 15);
label2.Size = new Size(200, 35);
panelTemp.Contr ols.Add(label2) ;
e.Result = panelTemp;
>
}
>
private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;
>
>
>
}
>
private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
}
>
>
}
>
private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar
>
backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();
>
}
>
private void button2_Click(o bject sender, EventArgs e)
{
>
// Cancel asynchronous processing
>
if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
>
>
}
>
private System.Componen tModel.IContain er components = null;
>
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}
>
#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
>
}
>
#endregion
>
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
Dec 19 '06 #4
Whoops, posted wrong code that is something I was working on :-)

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
//Label label1 = new Label();
//label1.Text = "Label 1 text from foreground thread";
//label1.Location = new Point(1, 1);
//label1.Size = new Size(200, 35);
//panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeReq uired)
{
this.panel1.Inv oke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread.";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 1);

Label label2 = new Label();
label2.Text = "Label 2 text from background thread.";
label2.Location = new Point(1, 35);
label2.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 2);
}
}
private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
/*
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
*/
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">
/// true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}

--
http://www.markdawson.org
"Code like the person who maintains your code is a psychotic axe murdered -
WHO KNOWS WHERE YOU LIVE"
Dec 19 '06 #5
That does the trick, but now I need to do one more thing. Is there a way
that I can pass parameters to the UpdatePanel method from the background
thread? I don't seem to be able to figure that out.

"Mark R. Dawson" wrote:
Whoops, posted wrong code that is something I was working on :-)

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
//Label label1 = new Label();
//label1.Text = "Label 1 text from foreground thread";
//label1.Location = new Point(1, 1);
//label1.Size = new Size(200, 35);
//panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeReq uired)
{
this.panel1.Inv oke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread.";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 1);

Label label2 = new Label();
label2.Text = "Label 2 text from background thread.";
label2.Location = new Point(1, 35);
label2.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 2);
}
}
private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
/*
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
*/
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">
/// true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}

--
http://www.markdawson.org
"Code like the person who maintains your code is a psychotic axe murdered -
WHO KNOWS WHERE YOU LIVE"

Dec 19 '06 #6
Sure, you can create your own delegate that you pass to the Invoke method,
then pass in the parameter values as an array of objects i.e.

//You can put whatever parameters you want in the delegate
//define this inside your form class
delegate void UpdatePanelDele gate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(str ing myParam)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{myParam});
}
else
{
//do the work using myParam
}
}

Hope that helps.
Mark.
--
http://www.markdawson.org
"Code like the person who maintains your code is an axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
That does the trick, but now I need to do one more thing. Is there a way
that I can pass parameters to the UpdatePanel method from the background
thread? I don't seem to be able to figure that out.

"Mark R. Dawson" wrote:
Whoops, posted wrong code that is something I was working on :-)

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
//Label label1 = new Label();
//label1.Text = "Label 1 text from foreground thread";
//label1.Location = new Point(1, 1);
//label1.Size = new Size(200, 35);
//panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeReq uired)
{
this.panel1.Inv oke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread.";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 1);

Label label2 = new Label();
label2.Text = "Label 2 text from background thread.";
label2.Location = new Point(1, 35);
label2.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 2);
}
}
private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;

}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
/*
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
*/
}
}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">
/// true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}

#endregion

private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}

--
http://www.markdawson.org
"Code like the person who maintains your code is a psychotic axe murdered -
WHO KNOWS WHERE YOU LIVE"
Dec 19 '06 #7
I'm still having problems updating my UI panel. It seems to be blank.
Basically I populate a panel named panelTemp with controls in background
thread and then issue the command below. I need to populate the controls I
want in the UI panel, in the background thread because it is slow to add all
the controls others otherwise. You original post worked but all the controls
where added to the panel in the UI thread, and not the background thread.

// call from background thread to update panel in UI
UpdatePanel(pan elTemp);

I've defined the delegate at the top of my form like so:
delegate void UpdatePanelDele gate(Panel p);
And my UpdatePanel Method looks like this:
private void UpdatePanel(Pan el p)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{p});
}
else
{
panel1 = p;
}
}

"Mark R. Dawson" wrote:
Sure, you can create your own delegate that you pass to the Invoke method,
then pass in the parameter values as an array of objects i.e.

//You can put whatever parameters you want in the delegate
//define this inside your form class
delegate void UpdatePanelDele gate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(str ing myParam)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{myParam});
}
else
{
//do the work using myParam
}
}

Hope that helps.
Mark.
--
http://www.markdawson.org
"Code like the person who maintains your code is an axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
That does the trick, but now I need to do one more thing. Is there a way
that I can pass parameters to the UpdatePanel method from the background
thread? I don't seem to be able to figure that out.

"Mark R. Dawson" wrote:
Whoops, posted wrong code that is something I was working on :-)
>
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
>
namespace WindowsApplicat ion1
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
//Label label1 = new Label();
//label1.Text = "Label 1 text from foreground thread";
//label1.Location = new Point(1, 1);
//label1.Size = new Size(200, 35);
//panel1.Controls .Add(label1);
>
}
>
private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread
>
UpdatePanel();
}
>
private void UpdatePanel()
{
if (this.InvokeReq uired)
{
this.panel1.Inv oke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread.";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 1);
>
Label label2 = new Label();
label2.Text = "Label 2 text from background thread.";
label2.Location = new Point(1, 35);
label2.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 2);
}
}
>
>
private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;
>
>
>
}
>
private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
/*
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
*/
}
>
>
}
>
private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar
>
backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();
>
}
>
private void button2_Click(o bject sender, EventArgs e)
{
>
// Cancel asynchronous processing
>
if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );
>
>
}
>
private System.Componen tModel.IContain er components = null;
>
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">
/// true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}
>
#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
>
}
>
#endregion
>
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.ProgressB ar backgroundProgr essBar;
private System.Componen tModel.Backgrou ndWorker backgroundWorke r1;
private System.Windows. Forms.Panel panel1;
}
}
>
--
http://www.markdawson.org
"Code like the person who maintains your code is a psychotic axe murdered -
WHO KNOWS WHERE YOU LIVE"
>
>
Dec 19 '06 #8
Hi Greg,
in the code below:

private void UpdatePanel(Pan el p)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{p});
}
else
{
panel1 = p;
}
}

you have already added panel1 to the controls collection of the form
previously, so that controls collection has a reference to the panel1 panel
object. When you say "panel1 = p" you are simply updating what the panel1
variable points to but the forms control collection still is pointing to the
original instance it does not get updated.
There are a couple of things you can do, if you have a number of controls
you want to add quickly then you can use the AddRange method, this accepts an
array of controls to add to another control, calling this once with an array
of all the controls you want to add will be very quick, probably doign away
with the need for you trying to use the thread, for example
myPanel.Control s.AddRange(...) .

Otherwise create the panel object in the main form, then from your
background worker thread you can add the controls one by one to the panel by
using the Invoke methodology, so each control is added to the panel in the
main UI thread (the panel was created in the main UI thread and is just a
class field), then when all of the controls have been added to the panel, you
can then add the panel to the control collection of the form, at which point
it will become visible. I would recommend using the AddRange method first
though to see if it is fast enough.

Mark.
--
http://www.markdawson.org
"Greg Larsen" wrote:
I'm still having problems updating my UI panel. It seems to be blank.
Basically I populate a panel named panelTemp with controls in background
thread and then issue the command below. I need to populate the controls I
want in the UI panel, in the background thread because it is slow to add all
the controls others otherwise. You original post worked but all the controls
where added to the panel in the UI thread, and not the background thread.

// call from background thread to update panel in UI
UpdatePanel(pan elTemp);

I've defined the delegate at the top of my form like so:
delegate void UpdatePanelDele gate(Panel p);
And my UpdatePanel Method looks like this:
private void UpdatePanel(Pan el p)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{p});
}
else
{
panel1 = p;
}
}

"Mark R. Dawson" wrote:
Sure, you can create your own delegate that you pass to the Invoke method,
then pass in the parameter values as an array of objects i.e.

//You can put whatever parameters you want in the delegate
//define this inside your form class
delegate void UpdatePanelDele gate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(str ing myParam)
{
if(this.panel1. InvokeRequired)
{
this.panel1.Inv oke(new UpdatePanelDele gate(UpdatePane l), new
object[]{myParam});
}
else
{
//do the work using myParam
}
}

Hope that helps.
Mark.
--
http://www.markdawson.org
"Code like the person who maintains your code is an axe murdered - WHO KNOWS
WHERE YOU LIVE"
"Greg Larsen" wrote:
That does the trick, but now I need to do one more thing. Is there a way
that I can pass parameters to the UpdatePanel method from the background
thread? I don't seem to be able to figure that out.
>
"Mark R. Dawson" wrote:
>
Whoops, posted wrong code that is something I was working on :-)

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{
class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
//Label label1 = new Label();
//label1.Text = "Label 1 text from foreground thread";
//label1.Location = new Point(1, 1);
//label1.Size = new Size(200, 35);
//panel1.Controls .Add(label1);

}

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs
e)
{
// Simulate a long-running task by putting the thread
// to sleep a random number of times. Each time the
// thread wakes up, report the progress.
BackgroundWorke r worker = (BackgroundWork er)sender;
System.Random rand = new Random();
int max = rand.Next(50, 500);
for (int i = 0; i < max; i++)
{
// Cancel upon cancellation requests
if (worker.Cancell ationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threadin g.Thread.Sleep( 30);
worker.ReportPr ogress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeReq uired)
{
this.panel1.Inv oke(new MethodInvoker(U pdatePanel));
}
else
{
Label label1 = new Label();
label1.Text = "Label 1 text from background thread.";
label1.Location = new Point(1, 1);
label1.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 1);

Label label2 = new Label();
label2.Text = "Label 2 text from background thread.";
label2.Location = new Point(1, 35);
label2.Size = new Size(200, 35);
this.panel1.Con trols.Add(label 2);
}
}


private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
backgroundProgr essBar.Value = e.ProgressPerce ntage;



}

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
MessageBox.Show (
this,
"Background processing completed with errors: " +
e.Error.Message ,
"ERROR!");
else if (e.Cancelled)
MessageBox.Show (
this,
"Background processing cancelled.",
"Cancelled" );
else
{
/*
panel1 = (Panel)e.Result ;
MessageBox.Show ("Number of controls on Panel1 - " +
panel1.Controls .Count.ToString ());
panel1.Invalida te();
panel1.Refresh( );
*/
}


}

private void button1_Click(o bject sender, EventArgs e)
{
// Reset progress bar

backgroundProgr essBar.Minimum = 0;
backgroundProgr essBar.Maximum = 100;
backgroundProgr essBar.Value = 0;
panel1.Controls .Clear();
// Initiate asynchronous processing
backgroundWorke r1.RunWorkerAsy nc();

}

private void button2_Click(o bject sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWork er1.IsBusy)
backgroundWorke r1.CancelAsync( );


}

private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">
/// true if managed resources should be disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.background ProgressBar = new
System.Windows. Forms.ProgressB ar();
this.background Worker1 = new
System.Componen tModel.Backgrou ndWorker();
this.panel1 = new System.Windows. Forms.Panel();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(24, 30);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(55, 22);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Us eVisualStyleBac kColor = true;
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(136, 30);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(59, 22);
this.button2.Ta bIndex = 1;
this.button2.Te xt = "button2";
this.button2.Us eVisualStyleBac kColor = true;
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// backgroundProgr essBar
//
this.background ProgressBar.Loc ation = new
System.Drawing. Point(24, 125);
this.background ProgressBar.Nam e = "backgroundProg ressBar";
this.background ProgressBar.Siz e = new System.Drawing. Size(229,
23);
this.background ProgressBar.Tab Index = 2;
//
// backgroundWorke r1
//
this.background Worker1.WorkerR eportsProgress = true;
this.background Worker1.WorkerS upportsCancella tion = true;
this.background Worker1.DoWork += new
System.Componen tModel.DoWorkEv entHandler(this .backgroundWork er1_DoWork);
this.background Worker1.RunWork erCompleted += new
System.Componen tModel.RunWorke rCompletedEvent Handler(this.ba ckgroundWorker1 _RunWorkerCompl eted);
this.background Worker1.Progres sChanged += new
System.Componen tModel.Progress ChangedEventHan dler(this.backg roundWorker1_Pr ogressChanged);
//
// panel1
//
this.panel1.Bac kColor =
System.Drawing. SystemColors.Co ntrolLightLight ;
this.panel1.Loc ation = new System.Drawing. Point(24, 177);
this.panel1.Nam e = "panel1";
this.panel1.Siz e = new System.Drawing. Size(238, 50);
this.panel1.Tab Index = 3;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.panel1) ;
this.Controls.A dd(this.backgro undProgressBar) ;
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
Dec 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4096
by: Franklin | last post by:
WITHOUT KNOWING ANYTHING ABOUT THE CURRENT COLORS, I want to swap the foreground/background colors of a link when someone hovers over it. Is this possible with HTML, CSS, DOM, & JavaScript? If so, how? See my HTML below (or go to my test page at http://fmbbowen.com:39353/misc/Misc.html). I also tried treating ..color as a number and subtracting the current color from 0xFFFFFF but that did not work either. The numerous examples I...
4
4335
by: Confused White Guy | last post by:
I have a C# winforms app in which I am calling a method on a different thread, using the Thread.Start() method. The method that is being executed on the new thread attempts to make a button visible and a panel visible after doing some work asynchronously. The button and panel are private to the form that contains both the method that starts the new thread (i.e. the calling method) and the method that the new thread executes. While...
4
2507
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
1
6985
by: Ioannis Vranos | last post by:
In .NET, what happens when a background thread and a foreground thread have the same priority (e.g. Normal). Do they share the same processor time? -- Ioannis Vranos
3
2537
by: Marcel van den Hof | last post by:
Dear all, Is there any way to prevent the ASP.NET worker process from recycling the worker process when a thread is being executed on the foreground (IsBackground=false). I'd also like to know if there a way for a foreground thread to detect if the worker process is being recycled? It seems that when a worker process recycles (under IIS 5.0) a thread
4
3109
by: Carsten Schmitt | last post by:
Hello, I want to draw a simple pixel (i.e. a red pixel in the center of the screen), which is always in the foreground - even when running a fullscreen application like a DirectX game. I need this as an easy entry for programming later a HUD, which shows several informations, like date, time, company logo, etc. Any help or just a simple code example would be appreciated.
0
2464
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly over the screen or I drag another window on top of the fast display, I get errors (Pyassertion ....). I believe those are related to a conflict between the thread that is updating the buffer (over which I have control) and the thread that is doing the...
0
4343
by: Czechtim | last post by:
Hello, I have problem with databinding. I created small application using structure that I need to demonstrate problem. I need to change content of label when changing content of property "Promena". I change content of property "Promena" from "Origin text" to "Changed text" using thread but content of label is still the same - databinding isn´t working according to my vision. Does somebody know, please, why content of label doesn´t change...
4
12023
by: BiffMaGriff | last post by:
Hello, I have a GridView that I put inside an update panel. I have a control that attaches to the datasource of the gridview that filters the data, databinds the gridview and then updates the update panel that the gridview is in. In my page render I put this so I could select a row by just clicking it. protected override void Render(HtmlTextWriter writer) {
0
8645
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7810
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5903
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4657
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.