Voilà un exemple concret.
Y'a peut-être plus simple mais je trouve ça clair.
La procédure ajoute 1 à l'infini.
On lui indique juste de renvoyer au programme l'état du
thread.
Si StopAA = 0 alors le
thread va s'arrêter.
StopAA = 0 sous 2 conditions
1) - Si on clique sur le bouton
2) - Si aa = 100
je peux pas faire plus clair.
Code : Tout sélectionner
; by Ar-S / 2011
; exemple de Thread avec
; Activation / arrêt via le même bouton
Enumeration
#Window_0
EndEnumeration
Enumeration
#BT
#Affiche
EndEnumeration
aa = 1 ; On initialise aa à 1
StopAA = 0 ; L'indicateur StopAA = 0 ce qui signifie que le thread n'est pas actif
Procedure.l a(*a)
Shared aa,StopAA
Repeat ; On répète l'opération jusqu'à ce que aa = 100 OU que StopAA = 0
SetGadgetText(#Affiche,Str(aa))
If aa = 100
SetGadgetText(#Affiche,"aa = " + Str(aa) + " ! Je renvoie StopAA = 1")
StopAA = 0
EndIf
Delay(50)
aa + 1
Until StopAA = 0
EndProcedure
If OpenWindow(#Window_0, 454, 216, 301, 82, "Thread", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
If CreateGadgetList(WindowID(#Window_0))
ButtonGadget(#BT, 5, 5, 290, 45, "GO")
StringGadget(#Affiche, 10, 55, 280, 20, "", #PB_String_ReadOnly)
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case #BT
; Si tu souhaites que le thread recompte à partir de 1
; tu réinitialises la valeur de aa
aa = 1
; on vérifie l'état de StopAA
If StopAA = 0
StopAA + 1 ; Etat Activé
SetGadgetText(#BT,"Clique pour arrêter (et tuer) le thread")
Else
StopAA = 0 ; Etat Désactivé
SetGadgetText(#BT,"GO")
EndIf
; Le thread se lance SI StopAA = 1
If StopAA = 1
thread = CreateThread(@a(),123)
Else ; Sinon on arrête le Thread (avec une petite vérif Isthread qui peut pas faire de mal)
If IsThread(thread)
KillThread(thread)
EndIf
EndIf
EndSelect
Case #PB_Event_CloseWindow
Select EventWindow()
Case #Window_0
If IsThread(thread)
KillThread(thread)
EndIf
CloseWindow(#Window_0)
Break
EndSelect
EndSelect
ForEver
EndIf