pywinrt/python-winsdk

Unable to complete pick_single_item_async

Closed this issue · 2 comments

Starting with the following snippet of code:

from winsdk.windows.graphics.capture import GraphicsCapturePicker, GraphicsCaptureItem
from winsdk._winrt import initialize_with_window

picker = GraphicsCapturePicker()
initialize_with_window(picker, self.effectiveWinId().__int__())  # Get the PyQt HWND

async_operation = await picker.pick_single_item_async() never gets past the await

async_operation = picker.pick_single_item_async()
while True:
    pass # check for completion

looking at the object's properties, status stays at 0 (STARTED), completed throws a not implemented error, get_results throws "OSError(22, 'A method was called at an unexpected time', None, -2147483634, None)", even after the picker is closed.

I'm not sure how I'm supposed to get the resulting GraphicsCaptureItem from GraphicsCapturePicker

Sidenote, the return type of pick_single_item_async is Unknown (even though it looks like from the stub file it should be a generic IAsyncOperation[GraphicsCaptureItem])
image

dlech commented

You either need to use asyncio so that you can write item = await picker.pick_singe_item_async() or you need to add a callback that will be called when the operation has completed with async_operation.completed = callback and then call async_operation.get_results() in the callback. Have a look at the link below for an example.

https://github.com/pywinrt/pywinrt/blob/9aec2c357da5ce125109ecb3dc7a161a0b5d336a/test/test_geoloc.py#L57-L82

Ah, that makes sense if completed is supposed to be a callback. It works that way thank you. Maybe I couldn't get await to work due to a lack of experience with asyncio, but I got it working this way:

def select_graphics_item(autosplit: AutoSplit):
    def callback(async_operation: IAsyncOperation[GraphicsCaptureItem], async_status: AsyncStatus):
        autosplit.graphics_capture_item = async_operation.get_results()

    picker = GraphicsCapturePicker()
    initialize_with_window(picker, autosplit.effectiveWinId().__int__())
    picker.pick_single_item_async().completed = callback

Thanks for the support. Hopefully this helps anyone else who comes across the same issue :)