共用方式為


IVsDataHostService.UIThread 屬性

取得執行 Visual Studio 處理序的主要 (UI) 執行緒。

命名空間:  Microsoft.VisualStudio.Data.Core
組件:  Microsoft.VisualStudio.Data.Core (在 Microsoft.VisualStudio.Data.Core.dll 中)

語法

'宣告
ReadOnly Property UIThread As Thread
Thread UIThread { get; }
property Thread^ UIThread {
    Thread^ get ();
}
abstract UIThread : Thread
function get UIThread () : Thread

屬性值

型別:System.Threading.Thread
在 Visual Studio 中執行處理序的主要 (UI) 執行緒。

備註

這個屬性來判斷程式碼是否適用於 Visual Studio 處理序的主執行緒正在執行中。這通常很有用圍繞呼叫 InvokeOnUIThread 或避免動態委派的最高限度值的 BeginInvokeOnUIThread 叫用方法。

範例

下列程式碼示範 UIThread 屬性的典型使用判斷在 UI 執行緒必須執行的方法會直接呼叫或必須動態叫用封送處理在執行緒之間的呼叫。

using System;
using System.Threading;
using Microsoft.VisualStudio.Data.Core;
using Microsoft.VisualStudio.Shell.Interop;

public class DdexHostSvcExample2
{
    public static void UpdateUI(IVsDataHostService hostService)
    {
        if (Thread.CurrentThread == hostService.UIThread)
        {
            // Called on UI thread, directly call method
            ActuallyUpdateUI(hostService);
        }
        else
        {
            // Called from background thread, begin invoke on UI thread
            hostService.BeginInvokeOnUIThread(
                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 命名空間