IVsDataHostService.BeginInvokeOnUIThread 方法

异步执行在主(用户界面)线程的指定方法在Visual Studio中运行处理,并指定的参数列表。

命名空间:  Microsoft.VisualStudio.Data.Core
程序集:  Microsoft.VisualStudio.Data.Core(在 Microsoft.VisualStudio.Data.Core.dll 中)

语法

声明
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

参数

  • method
    类型:System.Delegate
    一个方法委托,它采用的参数的数量和类型与 args 参数中所包含的相同。
  • args
    类型:array<System.Object[]
    作为指定方法的参数传递的对象数组。如果此方法没有参数,该参数可以是 nullnull 引用(在 Visual Basic 中为 Nothing)。

返回值

类型:System.IAsyncResult
表示此操作的结果 IAsyncResult 实例。

备注

此方法对于在后台线程上运行并定期需要通知更新UI线程发送操作的多线程方案很有用。这种情况包括引发由本机代码的单线程的COM组件处理的事件。

当调用此方法时,它将消息发送到UI线程的Windows消息队列,在处理调用指定的方法。此方法是异步的,这意味着调用的线程都立即返回,在传递消息时了。返回的 IAsyncResult 实例可由后台线程用于确定处理在UI线程上执行此消息时完成。

示例

下面的代码演示此方法的一用于告知无法从后台线程访问的本机Visual Studio服务。

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 安全性

请参见

参考

IVsDataHostService 接口

Microsoft.VisualStudio.Data.Core 命名空间