簡體   English   中英

COM異常未在C#中處理

[英]COM exception was unhandled in c#

我正在嘗試使用以下方法獲取自動化元素:

var automationElement = AutomationElement.FromPoint(location);

而且我得到了錯誤。

未處理COM異常:由於應用程序正在調度輸入同步調用,因此無法進行傳出調用。 (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))

誰能幫我這個忙.....

這很可能是線程問題。 如果您的程序試圖在其自己的用戶界面中查找元素,則需要在單獨的線程中進行操作。 試試看:

var automationElement;
Thread thread = new Thread(() =>
{
    automationElement = AutomationElement.FromPoint(location);
});
thread.Start();
thread.Join();
// now automationElemnt is set

它是第一次工作,但是之后就不工作了。

我已經使用mousehook獲取鼠標單擊時對象的屬性。 這是代碼。

    private  AutomationElement GetAutomationElementFromPoint(Point location)
    {


        AutomationElement automationElement =null;

        Thread thread = new Thread(() =>
        {
            automationElement = AutomationElement.FromPoint(location);
        });

        thread.Start();
        thread.Join();
        return automationElement;
    }

     private void mouseHook_MouseClick(object sender, MouseEventArgs e)
    {
        AutomationElement element = GetAutomationElementFromPoint(new System.Windows.Point(e.X, e.Y));

        //Thread.Sleep(900);
        if (element != null)
        {

           textBox1.Text =  "Name: " + element.Current.Name + " ID: " + element.Current.AutomationId + " Type: " + element.Current.LocalizedControlType;
        }
        else
           textBox1.Text = "Not found";
    }

第一次單擊將給出值,但在下次單擊時將給出空白值,即使element不為null。

有什么問題嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM