#🔒 Is there a better way?

7 messages · Page 1 of 1 (latest)

void thistle
#

I need to set time.sleep() after my start_instances() in order for every instance to be open so get_instances() works properly. But that's not "nice" since i would have to change the amount of seconds in time.sleep() depending on the amount of instances i want to open. Is there a better way?

import ctypes
from ctypes import wintypes
import subprocess

user32 = ctypes.windll.user32


def start_instances(instance_path, instance_cwd, instance_amount):
    processes = []
    for _ in range(instance_amount):
        process = subprocess.Popen(
            [(instance_path)],
            cwd=instance_cwd,
        )
        processes.append(process)
    print(f"\033[32m{processes}\033[0m")
    return processes


def get_instances(instance_title):
    instances = []

    def enum_handler(hWnd, lParam):
        # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-iswindowvisible
        if user32.IsWindowVisible(hWnd):
            # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextlengthw
            length = user32.GetWindowTextLengthW(hWnd)
            buffer = ctypes.create_unicode_buffer(length + 1)
            # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw
            user32.GetWindowTextW(hWnd, buffer, length + 1)
            instances.append((hWnd, buffer.value))
        return True

    # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows
    user32.EnumWindows(
        ctypes.WINFUNCTYPE(ctypes.c_bool, wintypes.HWND, wintypes.LPARAM)(enum_handler), 0
    )

    target_instances = [
        instance for instance in instances if instance_title in instance[1]
    ]

    print(f"\033[32m{target_instances}\033[0m")
    return target_instances
edgy prairieBOT
#

@void thistle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

void thistle
#

Is my question bad? Should i rephrase it

barren tinsel
#

Idk, is there a way to add a listener to when the instances have opened?
Like, even if you adjust time.sleep to a really big number, that will never be a 100% guarantee.

What you could do is just busy-wait until it got all instances.
You should probably also change your enum_handler function so that it doesn't always return True. Either make it return nothing (i.e. None) or give it a False path

crude hinge
#

Like Monke said it isn't 100% guaranteed, however to improve the current design you can do a retry of sorts where you get instances initially then do it again after a set time for x amount of times.

edgy prairieBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.