--- title: Make thread-safe calls to controls ms.date: "02/19/2019" description: Learn how to implement multithreading in your app by calling cross-thread controls in a thread-safe way. dev_langs: - "csharp" - "vb" f1_keywords: - "EHInvalidOperation.WinForms.IllegalCrossThreadCall" helpviewer_keywords: - "thread safety [Windows Forms], calling controls [Windows Forms]" - "calling controls [Windows Forms], thread safety [Windows Forms]" - "CheckForIllegalCrossThreadCalls property [Windows Forms]" - "Windows Forms controls [Windows Forms], multithreading" - "BackgroundWorker class [Windows Forms], examples" - "threading [Windows Forms], cross-thread calls" - "controls [Windows Forms], multithreading" ms.assetid: 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](/dotnet/standard/threading/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 method to call a delegate created in the main thread, which in turn calls the control. Or, you can implement a , 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 control. The `Button1_Click` event handler creates a new `WriteTextUnsafe` thread, which sets the main thread's property directly. ```csharp 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."; } ``` ```vb 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 with the message, **Cross-thread operation not valid. Control "" accessed from a thread other than the thread it was created on.** The 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 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: 1. The method, which calls a delegate from the main thread to call the control. 2. A 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](/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe) or [Build from the command line (Visual Basic)](/dotnet/visual-basic/reference/command-line-compiler/building-from-the-command-line). 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 *\.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 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 method with a delegate from the main thread, which makes the actual call to the control. The `SafeCallDelegate` enables setting the control's property. The `WriteTextSafe` method queries . If returns `true`, `WriteTextSafe` passes the `SafeCallDelegate` to the method to make the actual call to the control. If returns `false`, `WriteTextSafe` sets the directly. The `Button1_Click` event handler creates the new thread and runs the `WriteTextSafe` method. [!code-csharp[ThreadSafeCalls#1](~/samples/snippets/winforms/thread-safe/example1/cs/Form1.cs)] [!code-vb[ThreadSafeCalls#1](~/samples/snippets/winforms/thread-safe/example1/vb/Form1.vb)] ## Example: Use a BackgroundWorker event handler An easy way to implement multithreading is with the component, which uses an event-driven model. The background thread runs the event, which doesn't interact with the main thread. The main thread runs the and event handlers, which can call the main thread's controls. To make a thread-safe call by using , create a method in the background thread to do the work, and bind it to the event. Create another method in the main thread to report the results of the background work, and bind it to the or event. To start the background thread, call . The example uses the event handler to set the control's property. For an example using the event, see . [!code-csharp[ThreadSafeCalls#2](~/samples/snippets/winforms/thread-safe/example2/cs/Form1.cs)] [!code-vb[ThreadSafeCalls#2](~/samples/snippets/winforms/thread-safe/example2/vb/Form1.vb)] ## See also - - [How to: Run an operation in the background](how-to-run-an-operation-in-the-background.md) - [How to: Implement a form that uses a background operation](how-to-implement-a-form-that-uses-a-background-operation.md) - [Develop custom Windows Forms controls with the .NET Framework](developing-custom-windows-forms-controls.md)