6

If I create a thread that all it does is connect to some process and get its top window, then the program hangs.

I debugged it a little and it seems to get stuck in comtypes._compointer_base.from_params. This is the whole traceback:

...
-> self.top_win = self.app.top_window()
  c:\python27\lib\site-packages\pywinauto\application.py(1095)top_window()
-> backend=self.backend.name)
  c:\python27\lib\site-packages\pywinauto\findwindows.py(197)find_elements()
-> cache_enable=True)
  c:\python27\lib\site-packages\pywinauto\uia_element_info.py(272)children()
-> return self._get_elements(IUIA().tree_scope["children"], cond, cache_enable)
  c:\python27\lib\site-packages\pywinauto\uia_element_info.py(261)_get_elements()
-> ptrs_array = self._element.FindAll(tree_scope, cond)
> c:\python27\lib\site-packages\comtypes\__init__.py(970)from_param()
-> return value

after typing step in pdb, it shows this and then freezes:

(Pdb) s
--Return--
> c:\python27\lib\site-packages\comtypes\__init__.py(970)from_param()-><POINTER... 41308a0>
-> return value

It seems that the problem is in using comtypes with threads, I tried to call pythoncom.CoInitialize() in the calling thread (and also in main) but it didn't help.

What can be done here?

Thanks.

cydan
  • 615
  • 5
  • 17
  • 1
    Hmm... Good question. Maybe there are some pitfalls for threading in MS UI Automation also... If you could find answer, it might be twice useful because we're planning to do some threading for future "record-replay" implementation. – Vasily Ryabov Jul 30 '17 at 17:50
  • 1
    One more thing I suspect is that pywinauto creates only one `IUIA()` object (yeah, it's singleton). I know it's kinda anti-pattern, but I made it as a workaround for another problem which is probably not workarounded anyway. :) I'll think how to make `IUIA()` instantiating once per thread or allow unrestricted number of instances... – Vasily Ryabov Jul 30 '17 at 18:01
  • 1
    [Issue #394](https://github.com/pywinauto/pywinauto/issues/394) is a reminder for us. – Vasily Ryabov Jul 30 '17 at 18:04
  • Thanks.. For now, I made a workaround to make it single threaded – cydan Jul 31 '17 at 07:04

1 Answers1

2

pywinauto==0.6.4 uses multi-threading mode (MTA) for COM objects by default. Just upgrade it by pip install -U pywinauto and check if it works for you.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78