472,780 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Can't update foreground panel from background thread

I'm trying to figure out how to modify a panel (panel1) from a
backgroundworker 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 backgroundworker 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((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.Controls.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.Controls.Add(label2);
e.Result = panelTemp;

}

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private System.Windows.Forms.Panel panel1;
}
}
Dec 19 '06 #1
8 5243
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 backgroundWorker1_DoWork, do something
like:

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

private void UpdatePanel()
{
if(this.panelTemp.InvokeRequired)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.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.Controls.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
backgroundworker 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 backgroundworker 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((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.Controls.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.Controls.Add(label2);
e.Result = panelTemp;

}

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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 backgroundWorker1_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 backgroundWorker1_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 backgroundWorker1_DoWork, do something
like:

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

private void UpdatePanel()
{
if(this.panelTemp.InvokeRequired)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.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.Controls.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
backgroundworker 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 backgroundworker 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((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.Controls.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.Controls.Add(label2);
e.Result = panelTemp;

}

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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.Collections.Generic;
using System.ComponentModel;
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(Camera camera)
{
InitializeComponent();
this.camera = camera;
}

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

//TODO:CurrentImage 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 NotImplementedException("Only available in the " +
"premium edition of the viewer");
}
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Override this and do nothing so we don't get any flickering
if (this.currentImage == null)
{
base.OnPaintBackground(e);
}
}

protected override void OnPaint(PaintEventArgs e)
{

//TODO: Need to make sure we lock when we update the image
lock (this.imageLock)
{
if (this.currentImage != 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.currentImage.Width,
this.currentImage.Height);

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

//Draw the image, scaling as necessary to fit the
//whole control.
e.Graphics.DrawImage(this.currentImage,
destinationRegion,
sourceRegion,
GraphicsUnit.Pixel);
}
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 backgroundWorker1_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 backgroundWorker1_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 backgroundWorker1_DoWork, do something
like:

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

private void UpdatePanel()
{
if(this.panelTemp.InvokeRequired)
{
//being called from different thread than main UI thread,
//call this function again on the main thread.
this.panelTemp.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.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.Controls.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
backgroundworker 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 backgroundworker 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
>
namespace test
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((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.Controls.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.Controls.Add(label2);
e.Result = panelTemp;
>
}
>
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;
>
>
>
}
>
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
}
>
>
}
>
private void button1_Click(object sender, EventArgs e)
{
// Reset progress bar
>
backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();
>
}
>
private void button2_Click(object sender, EventArgs e)
{
>
// Cancel asynchronous processing
>
if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
>
>
}
>
private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
>
}
>
#endregion
>
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
Dec 19 '06 #4
Whoops, posted wrong code that is something I was working on :-)

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

namespace WindowsApplication1
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
//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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeRequired)
{
this.panel1.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.Add(label1);

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.Controls.Add(label2);
}
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
*/
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
//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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeRequired)
{
this.panel1.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.Add(label1);

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.Controls.Add(label2);
}
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
*/
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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 UpdatePanelDelegate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(string myParam)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
//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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeRequired)
{
this.panel1.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.Add(label1);

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.Controls.Add(label2);
}
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
*/
}
}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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(panelTemp);

I've defined the delegate at the top of my form like so:
delegate void UpdatePanelDelegate(Panel p);
And my UpdatePanel Method looks like this:
private void UpdatePanel(Panel p)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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 UpdatePanelDelegate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(string myParam)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
>
namespace WindowsApplication1
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
//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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread
>
UpdatePanel();
}
>
private void UpdatePanel()
{
if (this.InvokeRequired)
{
this.panel1.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.Add(label1);
>
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.Controls.Add(label2);
}
}
>
>
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;
>
>
>
}
>
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
*/
}
>
>
}
>
private void button1_Click(object sender, EventArgs e)
{
// Reset progress bar
>
backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();
>
}
>
private void button2_Click(object sender, EventArgs e)
{
>
// Cancel asynchronous processing
>
if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
>
>
}
>
private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
>
}
>
#endregion
>
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar backgroundProgressBar;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
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(Panel p)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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.Controls.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(panelTemp);

I've defined the delegate at the top of my form like so:
delegate void UpdatePanelDelegate(Panel p);
And my UpdatePanel Method looks like this:
private void UpdatePanel(Panel p)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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 UpdatePanelDelegate(string myParameter);

//Now when you call Invoke
private void UpdatePanel(string myParam)
{
if(this.panel1.InvokeRequired)
{
this.panel1.Invoke(new UpdatePanelDelegate(UpdatePanel), 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
class Form1 : Form
{
public Form1()
{
InitializeComponent();
//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 backgroundWorker1_DoWork(object 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.
BackgroundWorker worker = (BackgroundWorker)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.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// Sleep for 30 milliseconds
System.Threading.Thread.Sleep(30);
worker.ReportProgress((int)i * 100 / max);
}
}
// Pass up the number of iterations perform to the main thread

UpdatePanel();
}

private void UpdatePanel()
{
if (this.InvokeRequired)
{
this.panel1.Invoke(new MethodInvoker(UpdatePanel));
}
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.Controls.Add(label1);

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.Controls.Add(label2);
}
}


private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
backgroundProgressBar.Value = e.ProgressPercentage;



}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs 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.Invalidate();
panel1.Refresh();
*/
}


}

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

backgroundProgressBar.Minimum = 0;
backgroundProgressBar.Maximum = 100;
backgroundProgressBar.Value = 0;
panel1.Controls.Clear();
// Initiate asynchronous processing
backgroundWorker1.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)
{

// Cancel asynchronous processing

if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();


}

private System.ComponentModel.IContainer 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.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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.backgroundProgressBar = new
System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 30);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 22);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 22);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// backgroundProgressBar
//
this.backgroundProgressBar.Location = new
System.Drawing.Point(24, 125);
this.backgroundProgressBar.Name = "backgroundProgressBar";
this.backgroundProgressBar.Size = new System.Drawing.Size(229,
23);
this.backgroundProgressBar.TabIndex = 2;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new
System.ComponentModel.DoWorkEventHandler(this.back groundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new
System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new
System.ComponentModel.ProgressChangedEventHandler( this.backgroundWorker1_ProgressChanged);
//
// panel1
//
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(24, 177);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(238, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Controls.Add(this.backgroundProgressBar);
this.Controls.Add(this.button2);
this.Controls.Add(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
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...
4
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...
4
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...
1
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
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...
4
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...
0
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...
0
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...
4
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...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.