Comment éviter un double click sur un toolstrip pour arrêter un thread?
Bonsoir,
Dans l'exemple suivant démarrer Notepad++ en appuyant sur 'Démarrer' puis faire un click sur le bouton "arrêter" du form pour arrêter le thread notepad++. Par contre après avoir démarrer Notepad++ il faut cliquer deux fois sur le ToolStrip "Stop2" pour arrêter le thread !
Pourquoi ? Je n'arrive pas à faire l'opération "arrêter" en un seul click sur le toolstrip. C'est comme si le premier click servait à changer de fenêtre en passant du thread au menu toolstrip. C'est sur que c'est pas très convivial.
Pour visualiser le problème : Ouvrir une application form avec 2 boutons "Démarrer" et "Arrêter" et un ToolStrip avec un bouton que l'on appellera Stop2.
Le code de mon exemple :
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
| Imports System.Threading
Public Class Form1
Dim LeProcess1 As Integer
Public Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Démarrer.Click
Dim tasks As New List(Of Task)()
tasks.Add(Task.Run(AddressOf task3))
Await Task.WhenAll(tasks)
End Sub
Private Async Function task3() As Task 'démarre le thread
Using myProcess1 As New Process
Try
myProcess1.StartInfo.Arguments = vbNormalFocus
myProcess1.StartInfo.FileName = "notepad++"
myProcess1.StartInfo.WindowStyle = ProcessWindowStyle.Normal
'démare l'exe
LeProcess1 = Process.Start(myProcess1.StartInfo).Id
Thread.Sleep(300)
Catch ex As Exception
MessageBox.Show(ex.Message & "Stack Trace: " & vbCrLf & ex.StackTrace)
End Try
End Using
Await Task.WhenAll()
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Stop1.Click
Try
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(LeProcess1)
proc.Kill()
MsgBox("process " & LeProcess1 & " notepad++ " & " est arrêté")
Catch ex As Exception
'Continu sans générer d'erreur si ID est faux
End Try
End Sub
Private Sub Stop2_Click(sender As Object, e As EventArgs) Handles Stop2.Click
Try
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(LeProcess1)
proc.Kill()
MsgBox("process " & LeProcess1 & " notepad++ " & " est arrêté")
Catch ex As Exception
'Continu sans générer d'erreur si ID est faux
End Try
End Sub
End Class |
En vous remerciant pour vos suggestions.