Make safe calls between diferent threads
May 8th, 2009
1 comment
I use multithreding to present a loading or change database process. To prevent form control to force into an inconsistent state and/or deadlocks it is important to make comunication between threds in safe-mode. To call a control from another thread then control’s thread you need to using the Invoke method.
In the following code example implements thread-safe Invoke method:
These piece of code is from Microsoft Visual Studio 2008 Documentation
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace CrossThreadDemo
{
public class Form1 : Form
{
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text);
// This thread is used to demonstrate both thread-safe and
// unsafe ways to call a Windows Forms control.
private Thread demoThread = null;
// This BackgroundWorker is used to demonstrate the
// preferred way of performing asynchronous operations.
private BackgroundWorker backgroundWorker1;
private TextBox textBox1;
private Button setTextUnsafeBtn;
private Button setTextSafeBtn;
private Button setTextBackgroundWorkerBtn;
private System.ComponentModel.IContainer components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
// This event handler creates a background thread that
// attempts to set a Windows Forms control property
// directly.
private void setTextUnsafeBtn_Click
(object sender, EventArgs e)
{
// Create a background thread and start it.
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
// Continue in the main thread. Set a textbox value that
// would be overwritten by demoThread if it succeeded.
// This value will appear immediately, then two seconds
// later the background thread will try to make its
// change to the textbox.
textBox1.Text = "Written by the main thread.";
}
// This method is executed on the worker thread. It attempts
// to access the TextBox control directly, which is not safe.
private void ThreadProcUnsafe()
{
// Wait two seconds to simulate some background work
// being done.
Thread.Sleep(2000);
this.textBox1.Text =
"Written unsafely by the background thread.";
}
// This event handler creates a thread that calls a
// Windows Forms control in a thread-safe way.
private void setTextSafeBtn_Click(
object sender,
EventArgs e)
{
// Create a background thread and start it.
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
this.demoThread.Start();
// Continue in the main thread. Set a textbox value
// that will be overwritten by demoThread.
textBox1.Text = "Written by the main thread.";
}
// If the calling thread is different from the thread that
// created the TextBox control, this method passes in the
// the SetText method to the SetTextCallback delegate and
// passes in the delegate to the Invoke method.
private void ThreadProcSafe()
{
// Wait two seconds to simulate some background work
// being done.
Thread.Sleep(2000);
string text = "Written by the background thread.";
// Check if this method is running on a different thread
// than the thread that created the control.
if (this.textBox1.InvokeRequired)
{
// It's on a different thread, so use Invoke.
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke
(d, new object[] { text + " (Invoke)" });
}
else
{
// It's on the same thread, no need for Invoke
this.textBox1.Text = text + " (No Invoke)";
}
}
// This method is passed in to the SetTextCallBack delegate
// to set the Text property of textBox1.
private void SetText(string text)
{
this.textBox1.Text = text;
}
// This method starts BackgroundWorker by calling
// RunWorkerAsync. The Text property of the TextBox control
// is set by a method running in the main thread
// when BackgroundWorker raises the RunWorkerCompleted event.
private void setTextBackgroundWorkerBtn_Click(
object sender,
EventArgs e)
{
this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.RunWorkerAsync();
// Continue in the main thread.
textBox1.Text = "Written by the main thread.";
}
// This method does the work you want done in the background.
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Wait two seconds to simulate some background work
// being done.
Thread.Sleep(2000);
// You could use the same technique as in the
// ThreadProcSafe method to set textBox1.Text here, but
// the preferred method is to do it from the Completed
// event handler which runs in the same thread as the one
// that created the control.
}
// This method is called by BackgroundWorker's
// RunWorkerCompleted event. Because it runs in the
// main thread, it can safely set textBox1.Text.
private void backgroundWorker1_RunWorkerCompleted(
object sender,
RunWorkerCompletedEventArgs e)
{
this.textBox1.Text =
"Written by the main thread after the background thread completed.";
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.setTextUnsafeBtn = new System.Windows.Forms.Button();
this.setTextSafeBtn = new System.Windows.Forms.Button();
this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(360, 20);
this.textBox1.TabIndex = 0;
//
// setTextUnsafeBtn
//
this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55);
this.setTextUnsafeBtn.Name = "setTextUnsafeBtn";
this.setTextUnsafeBtn.TabIndex = 1;
this.setTextUnsafeBtn.Text = "Unsafe Call";
this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click);
//
// setTextSafeBtn
//
this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);
this.setTextSafeBtn.Name = "setTextSafeBtn";
this.setTextSafeBtn.TabIndex = 2;
this.setTextSafeBtn.Text = "Safe Call";
this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);
//
// setTextBackgroundWorkerBtn
//
this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55);
this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn";
this.setTextBackgroundWorkerBtn.TabIndex = 3;
this.setTextBackgroundWorkerBtn.Text = "Safe BW Call";
this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(388, 96);
this.Controls.Add(this.setTextBackgroundWorkerBtn);
this.Controls.Add(this.setTextSafeBtn);
this.Controls.Add(this.setTextUnsafeBtn);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
Unique visitors to post: 1