PyQt5中線程掛起終止和喚醒

2021-09-13 09:29:31 字數 4383 閱讀 7106

執行緒的終止問題一直是可比較頭疼的問題,今天找到了乙個比較好的例子,分享給大家

# 值變化訊號

handle =-1

defrun

(self)

:try

:# 這個目前我沒弄明白這裡寫法

self.handle = ctypes.windll.kernel32.openthread(

# @undefinedvariable

win32con.process_all_access,

false

,int

(qthread.currentthreadid())

)except exception as e:

print

('get thread handle failed'

, e)

print

('thread id'

,int

(qthread.currentthreadid())

)# 迴圈傳送訊號

for i in

range(1

,101):

print

('value'

, i)

self.valuechanged.emit(i)

qthread.sleep(1)

class

window

(qwidget)

:def

__init__

(self,

*args,

**kwargs)

:super

(window, self)

.__init__(

*args,

**kwargs)

# 垂直布局

layout = qvboxlayout(self)

self.progressbar = qprogressbar(self)

self.progressbar.setrange(0,

100)

layout.addwidget(self.progressbar)

self.startbutton = qpushbutton(

'開啟執行緒'

, self, clicked=self.onstart)

layout.addwidget(self.startbutton)

self.suspendbutton = qpushbutton(

'掛起執行緒'

, self, clicked=self.onsuspendthread, enabled=

false

) layout.addwidget(self.suspendbutton)

self.resumebutton = qpushbutton(

'恢復執行緒'

, self, clicked=self.onresumethread, enabled=

false

) layout.addwidget(self.resumebutton)

self.stopbutton = qpushbutton(

'終止執行緒'

, self, clicked=self.onstopthread, enabled=

false

) layout.addwidget(self.stopbutton)

# 當前執行緒id

print

('main id'

,int

(qthread.currentthreadid())

)# 子執行緒

self._thread = worker(self)

self._thread.finished.connect(self._thread.deletelater)

self._thread.valuechanged.connect(self.progressbar.setvalue)

defonstart

(self)

:print

('main id'

,int

(qthread.currentthreadid())

) self._thread.start(

)# 啟動執行緒

self.startbutton.setenabled(

false

) self.suspendbutton.setenabled(

true

) self.stopbutton.setenabled(

true

)def

onsuspendthread

(self)

:if self._thread.handle ==-1

:return

print

('handle is wrong'

) ret = suspendthread(self._thread.handle)

print

('掛起執行緒'

, self._thread.handle, ret)

self.suspendbutton.setenabled(

false

) self.resumebutton.setenabled(

true

)def

onresumethread

(self)

:if self._thread.handle ==-1

:return

print

('handle is wrong'

) ret = resumethread(self._thread.handle)

print

('恢復執行緒'

, self._thread.handle, ret)

self.suspendbutton.setenabled(

true

) self.resumebutton.setenabled(

false

)def

onstopthread

(self)

: self.startbutton.setenabled(

false

) self.suspendbutton.setenabled(

false

) self.resumebutton.setenabled(

false

) ret = ctypes.windll.kernel32.terminatethread(

# @undefinedvariable

self._thread.handle,0)

print

('終止執行緒'

, self._thread.handle, ret)

self.stopbutton.setenabled(

false

)def

closeevent

(self, event)

:if self._thread.isrunning():

self._thread.quit(

)# 強制

# self._thread.terminate()

del self._thread

super

(window, self)

.closeevent(event)

if __name__ ==

'__main__'

:import sys

import os

print

('pid'

, os.getpid())

w = window(

) w.show())

)

PyQt5中線程掛起終止和喚醒

執行緒的終止問題一直是可比較頭疼的問題,今天找到了乙個比較好的例子,分享給大家 valuechanged pyqtsignal int 值變化訊號 handle 1 def run self try 這個目前我沒弄明白這裡寫法 self.handle ctypes.windll.kernel32.o...

PYQT5 執行緒掛起方法

qthread執行緒掛起需要乙個qwaitcondition物件,並且需要傳入乙個qmutex物件作為引數,qmutex物件初始化的時候應該是鎖定狀態,否則將出現異常情況,參考qt多執行緒qwaitcondition的問題 然後再呼叫cond.wait 掛起執行緒。cond.wakeall 喚醒執行...

執行緒的掛起,喚醒和終止

如果是利用h afxbeginthread 建立執行緒,需要獲取控制代碼,h m hthread 執行緒的掛起 在createthread 等函式建立執行緒時,可以指定執行緒的掛起狀態,在執行過程中也可以掛起執行緒 dword suspendthread handle hthread hthread...