Thread ralentit la vitesse d'exécution
Bonjour,
J'ai un petit programme qui fait des calculs assez longs dans plusieurs boucles imbriquées. J'ai dû donc ajouter un bouton STOP pour pouvoir interrompre le programme. Pour que le bouton soit accessible, j'ai dû insérer Application.doevents() dans la boucle. Ça a donné à peu près ce que je voulais sauf que le premier click sur STOP ne fait que redonner la main à l'interface et le 2ème marche.
Je suis allé fouiner sur le forum pour découvrir qu'il ne fallait surtout pas utiliser le Application.doevents() mais utiliser un thread pour réaliser le calcul en boucle. J'ai réécrit mon programme avec un Thread. Ça marche, mais c'est devenu dramatiquement lent.
- Moins d'une minute Pour la version avec Application.doevents()
- Plus de 6 minutes pour la version avec le Thread
J'aimerais savoir si quelqu'un a une idée là-dessus.
Merci
Thread ralenti la vitesse d'exécution
Voici le code version Application.Doevents()
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| Public Class Form1
Dim N As Integer = 1
Dim Vasy As Boolean
Private Sub OkTMR2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkTMR2.Click
Dim CTR, PREDIV, PR2, POSTDIV As UShort
Dim T, Err As ULong
Vasy = True
TextTMR2.Text = ""
For CTR = 1 To 256
For Each PREDIV In {2, 4, 16}
For PR2 = 1 To 255
For POSTDIV = 1 To 16
T = CTR * PREDIV * (PR2 + 1) * POSTDIV * 4 / Fosc.Text
Err = Math.Abs(T - Periode.Text)
If Err <= Tolerence.Text Then
TextTMR2.AppendText(String.Format("{0,3:G} {1,3:G} {2,3:G} {3,3:G} {4:D} {5:D}" + vbCrLf, CTR, PREDIV, PR2 + 1, POSTDIV, T, Err))
End If
Next
Next
Next
StatBox.Text = CTR.ToString + "/256"
My.Application.DoEvents()
If Vasy = False Then
StatBox.Text = "Interrompu"
TextTMR2.AppendText("Interrompu")
Exit Sub
End If
Next
StatBox.Text = "Términé"
TextTMR2.AppendText("Términé")
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
End
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label5.Text = "Pour intérrompre, Cliquez deux fois sur STOP ou sur EXIT"
Label3.Text = " CTR PREDIV PR2+1 POSTDIV T ERR"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Vasy = False
End Sub
End Class |
et Voici le code avec le Thread
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Imports System
Imports System.Windows.Forms
Imports System.Threading
Public Structure Tmr2_Counters
Dim CTR, PREDIV, PR2, POSTDIV As UInt16
Dim T, Ecart As ULong
End Structure
Public Class Form1
Private mon_thread As Thread ' déclaration d'un Thread
Private Delegate Sub MonDélégué(ByVal T_C As Tmr2_Counters) ' déclaration d'un délégué d'affichage
Private Delegate Sub suivi(ByVal cc As UInt16) ' déclaration d'un délégué d'affichage
Dim Vasy As Boolean
Private Sub OkTMR2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkTMR2.Click
mon_thread = New Thread(AddressOf Recherche) 'créer le thread mais ne le démarre pas
mon_thread.Start()
End Sub
Private Sub Recherche()
Dim Tmr2_Counters_1 As Tmr2_Counters
Vasy = True
For Tmr2_Counters_1.CTR = 1 To 256
Invoke(New suivi(AddressOf aff_suivi), Tmr2_Counters_1.CTR)
For Each Tmr2_Counters_1.PREDIV In {2, 4, 16}
For Tmr2_Counters_1.PR2 = 1 To 255
For Tmr2_Counters_1.POSTDIV = 1 To 16
With Tmr2_Counters_1
.T = .CTR * .PREDIV * (.PR2 + 1) * .POSTDIV * 4 / Fosc.Text
.Ecart = Math.Abs(.T - Periode.Text)
If .Ecart <= Tolerence.Text Then
Invoke(New MonDélégué(AddressOf DéléguéAffichage), Tmr2_Counters_1)
End If
End With
Next
Next
Next
If Vasy = False Then
Exit Sub
End If
Next
End Sub
Private Sub aff_suivi(ByVal CTR As UInt16)
StatBox.Text = CTR.ToString + "/256"
End Sub
Private Sub DéléguéAffichage(ByVal T_C As Tmr2_Counters)
With T_C
TextTMR2.AppendText(String.Format("{0,3:G} {1,3:G} {2,3:G} {3,3:G} {4:D} {5:D}" + vbCrLf, .CTR, .PREDIV, .PR2 + 1, .POSTDIV, .T, .Ecart))
End With
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
End
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Text = " CTR PREDIV PR2+1 POSTDIV T ERR"
End Sub
Private Sub STOP_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles STOP_Button.Click
Vasy = False
End Sub
End Class |
merci,