经验 | 多线程的应用

经验 | 多线程的应用

RPA 对 Excel 使用单元格写入操作,因为含有公式会弹出更新值的窗体,而流程只会卡在当前控件位置,不会运行下个鼠标点击取消的控件,所以使用了多线程在单元格写入操作前开启,在单元格写入操作后关闭

经验 | 多线程的应用

import ubpa.iautomation as iautomation
import threading
import time

def threadmethod(self):
    while True:
        print(self.gv_event)
        time.sleep(5)
        if self.gv_event == False:
            break
        try:
            selectorJson = {"selector":[{"ControlType":"按钮","ControlTypeID":"0xC350","Index":"2"},{"ControlType":"对话框","ControlTypeID":"0xC370","Index":"1"}]}
            iautomation.do_click(win_class=r'XLMAIN',win_name=r'Microsoft Excel - 新版原始数据库201910.xlsx',selector=selectorJson,button=r'left',curson=r'center',offsetX=0,offsetY=0,times=1,run_mode=r'unctrl',search_depth=2,waitfor=10)
        except Exception as e:
            print(e)

def click_cancel_btn(self):
    self.gv_event = True
    t = threading.Thread(target=threadmethod,args = (self,))
    t.setDaemon(True)
    t.start()
    
def close(self):
    self.gv_event = False