none
Automation.AddAutomationPropertyChangedEventHandler braucht ewig RRS feed

  • Frage

  • Ich habe eine Programmzeile:

    Automation.AddAutomationPropertyChangedEventHandler(automationElement, TreeScope.Element, 
     new AutomationPropertyChangedEventHandler(OnPropertyUpdate), ExpandCollapsePattern.ExpandCollapseStateProperty);
    

    Diese braucht beim automationElement=MenuItem "Datei" vom notepad ca 5 Sekunden. Sie wirft keine Exception, aber braucht ewig. Warum? Was kann ich tun? Was mache ich falsch?

    BTW: Win7, 64 Bit, VS 2008, Bei allen anderen Elementen geht's meist recht fix...

    Montag, 28. Februar 2011 12:59

Alle Antworten

  • VS und .Net-Reflector sagen, die Funktion steht im WaitOne, und dank eines Timeouts darf der Code dann irgendwann weiterlaufen:

    //MS.Internal.Automation.QueueProcessor
    internal bool PostSyncWorkItem(QueueItem workItem)
    {
      SyncQueueItem item = new SyncQueueItem(workItem);
      this._q.Enqueue(item);
      this._ev.Set();
      return item._ev.WaitOne(0x7d0, false); //<= Hier steht das Programm und wartet endlos
    }
    
     
    
    

    Bloß warum?... Das verstehe ich nicht.

    Montag, 28. Februar 2011 13:55
  • Hallo G.,

    das sollte eigentlich (auch schnell) funktionieren. Vielleicht hattest Du den ersten Change noch nicht mitbekommen o.ä..

    Der zweite Change würde zum Beispiel dann erscheinen, wenn Du den Fokus wieder auf eine andere App setzt.
    Wichtig auch, dass das Changed-Ereignis in einem anderen Thread stattfindet, insofern dieser wegen [Quelle] in den Hauptthread gemarshallt werden muss.

    using System;
    using System.Diagnostics;
    using System.Windows.Automation;
    using System.Windows.Forms;
    
    namespace WinAutomationDemo
    {
     public partial class Form1 : Form
     {
      DateTime start = DateTime.Now;
      Process notepadProzess = new Process();
      AutomationElement dateiMenüElement;
      TextBox textBox = new TextBox();
    
      public Form1()
      {
       InitializeComponent();
       Controls.Add(textBox); textBox.Multiline = true;
       textBox.Dock = DockStyle.Fill;
       this.Shown += Form1_Shown;
       notepadProzess.StartInfo.FileName = "notepad.exe";
       notepadProzess.Start();
       notepadProzess.WaitForInputIdle();
    
       AutomationElement rootElement = AutomationElement.RootElement;
    
       AutomationElement hauptFensterElement = rootElement.FindFirst(
        TreeScope.Children, new PropertyCondition(
         AutomationElement.ProcessIdProperty, notepadProzess.Id));
    
       dateiMenüElement = hauptFensterElement.FindFirst(
        TreeScope.Descendants, new PropertyCondition(
         AutomationElement.AutomationIdProperty, "Item 1"));
    
       Automation.AddAutomationPropertyChangedEventHandler(
        dateiMenüElement, TreeScope.Element,
        new AutomationPropertyChangedEventHandler(OnPropertyChange),
        ExpandCollapsePattern.ExpandCollapseStateProperty);
      }
    
      private void OnPropertyChange(object src, AutomationPropertyChangedEventArgs e)
      {
       textBox.Invoke(new Action(()=>
        textBox.AppendText(e.Property.ProgrammaticName + " geändert!\r\n" +
        "Zeitdifferenz:" + (DateTime.Now - start).ToString() + "\r\n")));
      }
    
      private void Form1_Shown(object sender, EventArgs e)
      {
       //Das Datei-Menü erweitern
       ExpandCollapsePattern editECPat =
        dateiMenüElement.GetCurrentPattern(ExpandCollapsePattern.Pattern)
        as ExpandCollapsePattern;
       editECPat.Expand();
      }
     }
    }


    ciao Frank

    Montag, 28. Februar 2011 15:32
  • Hallo G.,

    das sollte eigentlich (auch schnell) funktionieren. Vielleicht hattest Du den ersten Change noch nicht mitbekommen o.ä..


    Hallo Frank,

    Das Problem ist nicht, dass ich einen Event nicht mitbekomme. Das Problem ist das, dass das Anmelden des Events zu lange dauert.

    Mein Projekt ist momentan eine Konsolenanwendung. nagut, die ruft grade trotzdem im Hauptthread Application.Run() auf, um ggf ne Messagequeue zur Verfügung zu stellen...  Der Nebenthread will den Event anmelden. Aber das Application.Run() hat momentan (meiner Meinung nach nichts zu tun). Der notepad ist ja auch in einem anderen Prozess...

    Montag, 28. Februar 2011 15:58