def attach(self, timeout, wait=True):
        if self.skype is not None and windll.user32.IsWindow(self.skype):
            return
        self.acquire()
        self.skype = None
        try:
            if not self.isAlive():
                try:
                    self.start()
                except __HOLE__:
                    raise SkypeAPIError('Skype API closed')
                # wait till the thread initializes
                while not self.hwnd:
                    time.sleep(0.01)
            self.logger.debug('broadcasting SkypeControlAPIDiscover')
            fhwnd = self.get_foreground_window()
            try:
                if fhwnd:
                    windll.user32.SetForegroundWindow(self.hwnd)
                if not windll.user32.SendMessageTimeoutA(HWND_BROADCAST, self.SkypeControlAPIDiscover,
                                                         self.hwnd, None, 2, 5000, None):
                    raise SkypeAPIError('Could not broadcast Skype discover message')
                # wait (with timeout) till the WindProc() attaches
                self.wait = True
                t = threading.Timer(timeout2float(timeout), lambda: setattr(self, 'wait', False))
                if wait:
                    t.start()
                while self.wait and self.attachment_status not in (apiAttachSuccess, apiAttachRefused):
                    if self.attachment_status == apiAttachPendingAuthorization:
                        # disable the timeout
                        t.cancel()
                    elif self.attachment_status == apiAttachAvailable:
                        # rebroadcast
                        self.logger.debug('broadcasting SkypeControlAPIDiscover')
                        windll.user32.SetForegroundWindow(self.hwnd)
                        if not windll.user32.SendMessageTimeoutA(HWND_BROADCAST, self.SkypeControlAPIDiscover,
                                                                 self.hwnd, None, 2, 5000, None):
                            raise SkypeAPIError('Could not broadcast Skype discover message')
                    time.sleep(0.01)
                t.cancel()
            finally:
                if fhwnd:
                    windll.user32.SetForegroundWindow(fhwnd)
        finally:
            self.release()
        # check if we got the Skype window's hwnd
        if self.skype is not None:
            command = Command('PROTOCOL %s' % self.protocol, Blocking=True)
            self.send_command(command)
            self.protocol = int(command.Reply.rsplit(None, 1)[-1])
        elif not self.wait:
            raise SkypeAPIError('Skype attach timeout')
dataset/ETHPy150Open Skype4Py/Skype4Py/Skype4Py/api/windows.py/SkypeAPI.attach