Closed thelosttimes closed 1 year ago
先调用CoInitialize
没调用报这个错
import threading
import wxRobot from wxRobot import WeChatRobot
def send_message(): result = robot.SendText("filehelper", "test") print("send success: {}".format(result))
if name == "main": pid_list = wxRobot.get_wechat_pid_list() if len(pid_list) == 0: robot = wxRobot.start_wechat() pid_list.append(robot.pid) else: robot = WeChatRobot(pid_list[0]) try: robot.StartService() robot.ChangeWeChatVer("3.7.6") robot.StartReceiveMessage(0) threading.Thread(target=send_message).start() wxRobot.register_msg_event(pid_list[0]) except Exception as e: print('ex: ', e) robot.StopService()
Exception in thread Thread-1 (send_message): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\desktop\wechat\main.py", line 8, in send_message result = robot.SendText("filehelper", "test") File "C:\Users\Administrator\Desktop\desktop\wechat\wxRobot.py", line 329, in SendText return self.robot.CSendText(self.pid, receiver, msg) _ctypes.COMError: (-2147221008, '尚未调用 CoInitialize。', (None, None, None, 0, None))
调用之后报这个错
import threading
import comtypes
import wxRobot from wxRobot import WeChatRobot
def send_message(): comtypes.CoInitialize() result = robot.SendText("filehelper", "test") print("send success: {}".format(result)) comtypes.CoUninitialize()
if name == "main": pid_list = wxRobot.get_wechat_pid_list() if len(pid_list) == 0: robot = wxRobot.start_wechat() pid_list.append(robot.pid) else: robot = WeChatRobot(pid_list[0]) try: robot.StartService() robot.ChangeWeChatVer("3.7.6") robot.StartReceiveMessage(0) threading.Thread(target=send_message).start() wxRobot.register_msg_event(pid_list[0]) except Exception as e: print('ex: ', e) robot.StopService()
======== RESTART: C:\Users\Administrator\Desktop\desktop\wechat\main.py ======== Exception in thread Thread-1 (send_message): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\desktop\wechat\main.py", line 11, in send_message result = robot.SendText("filehelper", "test") File "C:\Users\Administrator\Desktop\desktop\wechat\wxRobot.py", line 329, in SendText return self.robot.CSendText(self.pid, receiver, msg) _ctypes.COMError: (-2147417842, '应用程序调用一个已为另一线程整理的接口。', (None, None, None, 0, None))
实在是不知道怎么写了
报错信息很明显了。main下面写的逻辑挪到线程函数里面去
我想多线程共享robot,如果这样的话,创建线程就没意义了呀
解决了,感谢大佬的解答
想了解下你最后用了啥方式,多线程共享robot?
@thelosttimes 代码贴下,怎么处理的,我是想在发消息的处理消息的时候异步
我确定我已经查看了 (标注
[ ]
为[x]
)我要申请 (标注
[ ]
为[x]
)