.NET Framework Class Library
ControlInvokeRequired Property

Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax
<[%$TOPIC/a82t6122_en-us_VS_110_1_0_0_0_0%](False)> _
Public ReadOnly Property InvokeRequired As [%$TOPIC/a82t6122_en-us_VS_110_1_0_0_0_1%]
[[%$TOPIC/a82t6122_en-us_VS_110_1_0_1_0_0%](false)]
public [%$TOPIC/a82t6122_en-us_VS_110_1_0_1_0_1%] InvokeRequired { get; }
[[%$TOPIC/a82t6122_en-us_VS_110_1_0_2_0_0%](false)]
public:
virtual property [%$TOPIC/a82t6122_en-us_VS_110_1_0_2_0_1%] InvokeRequired {
	[%$TOPIC/a82t6122_en-us_VS_110_1_0_2_0_2%] get () sealed;
}
[<[%$TOPIC/a82t6122_en-us_VS_110_1_0_3_0_0%](false)>]
abstract InvokeRequired : [%$TOPIC/a82t6122_en-us_VS_110_1_0_3_0_1%] with get
[<[%$TOPIC/a82t6122_en-us_VS_110_1_0_3_0_2%](false)>]
override InvokeRequired : [%$TOPIC/a82t6122_en-us_VS_110_1_0_3_0_3%] with get

Property Value

Type: SystemBoolean
true if the control's Handle was created on a different thread than the calling thread (indicating that you must make calls to the control through an invoke method); otherwise, false.

Implements

ISynchronizeInvokeInvokeRequired
Remarks

Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.

NoteNote

In addition to the InvokeRequired property, there are four methods on a control that are thread safe to call: Invoke, BeginInvoke, EndInvoke and CreateGraphics if the handle for the control has already been created. Calling CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls. For all other method calls, you should use one of these invoke methods when calling from a different thread.

If the control's handle does not yet exist, InvokeRequired searches up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, the InvokeRequired method returns false.

This means that InvokeRequired can return false if Invoke is not required (the call occurs on the same thread), or if the control was created on a different thread but the control's handle has not yet been created.

In the case where the control's handle has not yet been created, you should not simply call properties, methods, or events on the control. This might cause the control's handle to be created on the background thread, isolating the control on a thread without a message pump and making the application unstable.

You can protect against this case by also checking the value of IsHandleCreated when InvokeRequired returns false on a background thread. If the control handle has not yet been created, you must wait until it has been created before calling Invoke or BeginInvoke. Typically, this happens only if a background thread is created in the constructor of the primary form for the application (as in Application.Run(new MainForm()), before the form has been shown or Application.Run has been called.

One solution is to wait until the form's handle has been created before starting the background thread. Either force handle creation by calling the Handle property, or wait until the Load event to start the background process.

An even better solution is to use the SynchronizationContext returned by SynchronizationContext rather than a control for cross-thread marshaling.

NoteNote

An exception might be thrown if the thread that should process the message is no longer active.

For more information about multithreaded Windows Forms controls, see How to: Use a Background Thread to Search for Files and How to: Make Thread-Safe Calls to Windows Forms Controls.

Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.