Compartir a través de


IVsDataHostService.BeginInvokeOnUIThread Method

Asynchronously executes a specified method on the main (UI) 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 BeginInvokeOnUIThread ( _
    method As Delegate, _
    ParamArray args As Object() _
) As IAsyncResult
IAsyncResult BeginInvokeOnUIThread(
    Delegate method,
    params Object[] args
)
IAsyncResult^ BeginInvokeOnUIThread(
    Delegate^ method, 
    ... array<Object^>^ args
)
abstract BeginInvokeOnUIThread : 
        method:Delegate * 
        args:Object[] -> IAsyncResult
function BeginInvokeOnUIThread(
    method : Delegate, 
    ... args : Object[]
) : IAsyncResult

Parameters

  • method
    Type: System.Delegate

    A 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.IAsyncResult
An IAsyncResult instance that represents the result of this operation.

Remarks

This method is useful for multithreaded scenarios that do work on a background thread and periodically need to notify the UI thread of updates to the operation. Such scenarios include raising events that are handled by single-threaded COM components in native code.

When this method is called, it posts a message to the UI thread’s Windows message queue, which on processing calls the specified method. This method is asynchronous, meaning the calling thread is returns immediately once the message has been posted. The returned IAsyncResult instance can be used by the background thread to determine when processing of this message on the UI thread has completed.

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 DdexHostSvcExample3
{
    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

See Also

Reference

IVsDataHostService Interface

Microsoft.VisualStudio.Data.Core Namespace