IVsDataHostService.InvokeOnUIThread Method
Synchronously executes a specified method on the main thread running in the Visual Studio process, with the specified list of arguments.
Namespace: Microsoft.VisualStudio.Data.Core
Assembly: Microsoft.VisualStudio.Data.Core (in Microsoft.VisualStudio.Data.Core.dll)
Syntax
'Declaration
Function InvokeOnUIThread ( _
method As Delegate, _
ParamArray args As Object() _
) As Object
Object InvokeOnUIThread(
Delegate method,
params Object[] args
)
Object^ InvokeOnUIThread(
Delegate^ method,
... array<Object^>^ args
)
abstract InvokeOnUIThread :
method:Delegate *
args:Object[] -> Object
function InvokeOnUIThread(
method : Delegate,
... args : Object[]
) : Object
Parameters
method
Type: System.DelegateA delegate to a method that takes parameters of the same number and type that are contained in the args parameter.
args
Type: array<System.Object[]An array of objects to pass as arguments to the specified method. This parameter can be nulla null reference (Nothing in Visual Basic) if the method takes no arguments.
Return Value
Type: System.Object
An Object that contains the return value from the delegate being invoked, or nulla null reference (Nothing in Visual Basic) if the delegate has no return value.
Exceptions
Exception | Condition |
---|---|
[any] | The specified method threw an exception. |
Remarks
This method is useful for multithreaded scenarios that do the majority of work on a background thread but periodically need to execute code that will run only on the UI thread. Such code includes accessing global Visual Studio services, especially those implemented as single-threaded COM components in native code.
When this method is called, it adds a message to the UI thread’s windows message queue, which on processing calls the specified method. This method is synchronous, meaning the calling thread is blocked until the UI thread has completed processing the method.
Warning
Calling this method when the UI thread is waiting for a lock on an object that the calling thread currently has locked will cause a deadlock in the process. Following best practices for locking across multiple threads will help to minimize the occurrence of this problem.
Examples
The following code demonstrates one use of this method to call a native Visual Studio service that cannot be accessed from a background thread.
using System;
using System.Threading;
using Microsoft.VisualStudio.Data.Core;
using Microsoft.VisualStudio.Shell.Interop;
public class DdexHostSvcExample7
{
public static void UpdateUI(IVsDataHostService hostService)
{
if (hostService.UIThread == Thread.CurrentThread)
{
// Called on UI thread, directly call method
ActuallyUpdateUI(hostService);
}
else
{
// Called from background thread, invoke on UI thread
hostService.InvokeOnUIThread(
new UpdateUIDelegate(ActuallyUpdateUI),
hostService);
}
}
private delegate void UpdateUIDelegate(IVsDataHostService hostService);
private static void ActuallyUpdateUI(IVsDataHostService hostService)
{
IVsUIShell uiShell = hostService.GetService<IVsUIShell>();
uiShell.UpdateCommandUI(0); // fImmediateUpdate == false
}
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.