8.1 KiB
title, ms.date, description, dev_langs, f1_keywords, helpviewer_keywords, ms.assetid
| title | ms.date | description | dev_langs | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Make thread-safe calls to controls | 02/19/2019 | Learn how to implement multithreading in your app by calling cross-thread controls in a thread-safe way. |
|
|
|
138f38b6-1099-4fd5-910c-390b41cbad35 |
How to: Make thread-safe calls to Windows Forms controls
Multithreading can improve the performance of Windows Forms apps, but access to Windows Forms controls isn't inherently thread-safe. Multithreading can expose your code to very serious and complex bugs. Two or more threads manipulating a control can force the control into an inconsistent state and lead to race conditions, deadlocks, and freezes or hangs. If you implement multithreading in your app, be sure to call cross-thread controls in a thread-safe way. For more information, see Managed threading best practices.
There are two ways to safely call a Windows Forms control from a thread that didn't create that control. You can use the xref:System.Windows.Forms.Control.Invoke%2A?displayProperty=fullName method to call a delegate created in the main thread, which in turn calls the control. Or, you can implement a xref:System.ComponentModel.BackgroundWorker?displayProperty=nameWithType, which uses an event-driven model to separate work done in the background thread from reporting on the results.
Unsafe cross-thread calls
It's unsafe to call a control directly from a thread that didn't create it. The following code snippet illustrates an unsafe call to the xref:System.Windows.Forms.TextBox?displayProperty=nameWithType control. The Button1_Click event handler creates a new WriteTextUnsafe thread, which sets the main thread's xref:System.Windows.Forms.TextBox.Text%2A?displayProperty=nameWithType property directly.
private void Button1_Click(object sender, EventArgs e)
{
thread2 = new Thread(new ThreadStart(WriteTextUnsafe));
thread2.Start();
}
private void WriteTextUnsafe()
{
textBox1.Text = "This text was set unsafely.";
}
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Thread2 = New Thread(New ThreadStart(AddressOf WriteTextUnsafe))
Thread2.Start()
End Sub
Private Sub WriteTextUnsafe()
TextBox1.Text = "This text was set unsafely."
End Sub
The Visual Studio debugger detects these unsafe thread calls by raising an xref:System.InvalidOperationException with the message, Cross-thread operation not valid. Control "" accessed from a thread other than the thread it was created on. The xref:System.InvalidOperationException always occurs for unsafe cross-thread calls during Visual Studio debugging, and may occur at app runtime. You should fix the issue, but you can disable the exception by setting the xref:System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls%2A?displayProperty=nameWithType property to false.
Safe cross-thread calls
The following code examples demonstrate two ways to safely call a Windows Forms control from a thread that didn't create it:
- The xref:System.Windows.Forms.Control.Invoke%2A?displayProperty=fullName method, which calls a delegate from the main thread to call the control.
- A xref:System.ComponentModel.BackgroundWorker?displayProperty=nameWithType component, which offers an event-driven model.
In both examples, the background thread sleeps for one second to simulate work being done in that thread.
You can build and run these examples as .NET Framework apps from the C# or Visual Basic command line. For more information, see Command-line building with csc.exe or Build from the command line (Visual Basic).
Starting with .NET Core 3.0, you can also build and run the examples as Windows .NET Core apps from a folder that has a .NET Core Windows Forms <folder name>.csproj project file.
Example: Use the Invoke method with a delegate
The following example demonstrates a pattern for ensuring thread-safe calls to a Windows Forms control. It queries the xref:System.Windows.Forms.Control.InvokeRequired%2A?displayProperty=fullName property, which compares the control's creating thread ID to the calling thread ID. If the thread IDs are the same, it calls the control directly. If the thread IDs are different, it calls the xref:System.Windows.Forms.Control.Invoke%2A?displayProperty=nameWithType method with a delegate from the main thread, which makes the actual call to the control.
The SafeCallDelegate enables setting the xref:System.Windows.Forms.TextBox control's xref:System.Windows.Forms.TextBox.Text%2A property. The WriteTextSafe method queries xref:System.Windows.Forms.Control.InvokeRequired%2A. If xref:System.Windows.Forms.Control.InvokeRequired%2A returns true, WriteTextSafe passes the SafeCallDelegate to the xref:System.Windows.Forms.Control.Invoke%2A method to make the actual call to the control. If xref:System.Windows.Forms.Control.InvokeRequired%2A returns false, WriteTextSafe sets the xref:System.Windows.Forms.TextBox.Text%2A?displayProperty=nameWithType directly. The Button1_Click event handler creates the new thread and runs the WriteTextSafe method.
[!code-csharpThreadSafeCalls#1] [!code-vbThreadSafeCalls#1]
Example: Use a BackgroundWorker event handler
An easy way to implement multithreading is with the xref:System.ComponentModel.BackgroundWorker?displayProperty=nameWithType component, which uses an event-driven model. The background thread runs the xref:System.ComponentModel.BackgroundWorker.DoWork?displayProperty=nameWithType event, which doesn't interact with the main thread. The main thread runs the xref:System.ComponentModel.BackgroundWorker.ProgressChanged?displayProperty=nameWithType and xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted?displayProperty=nameWithType event handlers, which can call the main thread's controls.
To make a thread-safe call by using xref:System.ComponentModel.BackgroundWorker, create a method in the background thread to do the work, and bind it to the xref:System.ComponentModel.BackgroundWorker.DoWork event. Create another method in the main thread to report the results of the background work, and bind it to the xref:System.ComponentModel.BackgroundWorker.ProgressChanged or xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted event. To start the background thread, call xref:System.ComponentModel.BackgroundWorker.RunWorkerAsync%2A?displayProperty=nameWithType.
The example uses the xref:System.ComponentModel.BackgroundWorker.RunWorkerCompleted event handler to set the xref:System.Windows.Forms.TextBox control's xref:System.Windows.Forms.TextBox.Text%2A property. For an example using the xref:System.ComponentModel.BackgroundWorker.ProgressChanged event, see xref:System.ComponentModel.BackgroundWorker.
[!code-csharpThreadSafeCalls#2] [!code-vbThreadSafeCalls#2]