import os import time import threading import psutil import pygetwindow as gw # Cần cài…
- User
import os import time import threading import psutil import pygetwindow as gw # Cần cài đặt thư viện pygetwindow
def is_process_running(process_name): for process in psutil.process_iter(['pid', 'name']): if process.info['name'] == process_name: return True return False
def is_process_ready(process_name): if not is_process_running(process_name): print(f"Đang chờ tiến trình {process_name} khởi chạy...") process_ready = False while not process_ready: for process in psutil.process_iter(['pid', 'name', 'status']): if process.info['name'] == process_name: if process.info['status'] == 'running': process_ready = True print(f"Tiến trình {process_name} đã sẵn sàng !") return True else: time.sleep(1) return False
def close_unique_zalo_window(): zalo_windows = gw.getWindowsWithTitle("Zalo")
if zalo_windows: for window in zalo_windows: if window.title == "Zalo": window.close() return True return Falsedef consumer(): # Kiểm tra xem tiến trình Zalo.exe đã sẵn sàng chưa is_process_ready("Zalo.exe")
# Kiểm tra xem tiến trình ZaloCall.exe đã sẵn sàng chưa is_process_ready("ZaloCall.exe") countdown = 3 # Đếm ngược mặc định là 3 giây closed_notification_shown = False # Trạng thái của thông báo while True: if close_unique_zalo_window(): countdown = 3 # Nếu cửa sổ đã được đóng, reset lại đếm ngược if not closed_notification_shown: print("Cửa sổ Zalo đã đóng ! ") closed_notification_shown = True else: # Đặt lại trạng thái thông báo khi cửa sổ Zalo mở lại closed_notification_shown = False print(f"Cửa sổ tự thoát sau {countdown} giây...") time.sleep(1) countdown -= 1 if countdown == 0: print("Đếm ngược kết thúc. Thoát chương trình.") os._exit(0) # Sử dụng os._exit(0) để thoát mà không gây ra lỗi hiển thịif name == "main": # Bắt đầu luồng tiêu thụ (consumer) consumer_thread = threading.Thread(target=consumer) consumer_thread.daemon = True consumer_thread.start()
try: while True: # Thực hiện các công việc khác nếu cần time.sleep(1) except KeyboardInterrupt: os._exit(0) - Assistant
- User
tôi muốn thêm lệnh bấm enter rồi mới chạy script
- Assistant
Để thêm yêu cầu nhập một phím (ví dụ phím "Enter") trước khi bắt đầu chạy chương trình, bạn có thể sử dụng
input()trong Python, nó sẽ tự động chờ người dùng nhấn Enter để tiếp tục. Dưới đây là mã nguồn đã sửa đổi để đáp ứng yêu cầu của bạn:# Các thư viện cần thiết đã được import như mã nguồn gốc # Định nghĩa các hàm is_process_running, is_process_ready, close_unique_zalo_window và consumer như cũ if __name__ == "__main__": input("Nhấn Enter để bắt đầu chạy script...") # Thêm dòng này vào mã nguồn # Bắt đầu luồng tiêu thụ (consumer) consumer_thread = threading.Thread(target=consumer) consumer_thread.daemon = True consumer_thread.start() try: while True: # Thực hiện các công việc khác nếu cần time.sleep(1) except KeyboardInterrupt: os._exit(0)Bằng cách thêm dòng
input("Nhấn Enter để bắt đầu chạy script..."), chương trình sẽ hiển thị thông báo và chờ cho đến khi người dùng nhấn phím "Enter". Ngay sau khi người dùng nhấn "Enter", chương trình sẽ khởi chạy luồng và tiếp tục thực hiện công việc. - User
đoạn mã đầy đủ
- Assistant