简体   繁体   中英

COM exception was unhandled in c#

I am trying to get Automation Element using:

var automationElement = AutomationElement.FromPoint(location);

And I am geting error.

COM exception was unhandled: An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))

Can any one help me out with this please.....

It is most likely a threading issue. If your program is trying to find an element in its own user interface you need do it in a separate thread. Give this a try:

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

It is working for 1st time but after that it is not working....

I have use mousehook to get property of object on mouse click. Here is code.

    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";
    }

On 1st click it is giving values, but on next click it gives blank values even if element is not null.

What can be problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM