Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 655f600

Browse files
committed
Don't use thread.exit_prog(); set a global variable to stop other
thread
1 parent dc3c53c commit 655f600

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

Demo/threads/wpi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Display digits of pi in a window, calculating in a separate thread.
22
# Compare ../scripts/pi.py.
33

4+
import sys
45
import time
56
import thread
67
import stdwin
78
from stdwinevents import *
89

10+
ok = 1
11+
912
digits = []
1013

1114
def worker():
1215
k, a, b, a1, b1 = 2l, 4l, 1l, 12l, 4l
13-
while 1:
16+
while ok:
1417
# Next approximation
1518
p, q, k = k*k, 2l*k+1l, k+1l
1619
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
@@ -23,11 +26,13 @@ def worker():
2326
d, d1 = a/b, a1/b1
2427

2528
def main():
29+
global ok
2630
digits_seen = 0
2731
thread.start_new_thread(worker, ())
2832
tw = stdwin.textwidth('0 ')
2933
lh = stdwin.lineheight()
3034
stdwin.setdefwinsize(20 * tw, 20 * lh)
35+
stdwin.setdefscrollbars(0, 1)
3136
win = stdwin.open('digits of pi')
3237
options = win.menucreate('Options')
3338
options.additem('Auto scroll')
@@ -37,7 +42,8 @@ def main():
3742
win.settimer(1)
3843
type, w, detail = stdwin.getevent()
3944
if type == WE_CLOSE:
40-
thread.exit_prog(0)
45+
ok = 0
46+
sys.exit(0)
4147
elif type == WE_DRAW:
4248
(left, top), (right, bottom) = detail
4349
digits_seen = len(digits)

0 commit comments

Comments
 (0)