Stopper le backgroundworker

Résolu
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - Modifié par cs_ShayW le 26/01/2014 à 23:57
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - 28 janv. 2014 à 21:58
Bonjour,

J'esssaie de stopper le backgroundworker
'le clique doit stopper le backgroundworker
Private Sub ToolStripButtonNew_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripButtonNew.MouseDown
backgroundWorker1.WorkerSupportsCancellation = True
backgroundWorker1.CancelAsync()
End Sub

Private Sub Buttonvalid_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Buttonvalid.MouseClick
backgroundWorker1.RunWorkerAsync()
end sub

Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork

If backgroundWorker1.CancellationPending Then
e.Cancel = True
End If
end sub

le résultat si je clique sur le toolStripButtonNew et ensuite sur le boutonvalid
une exception à backgroundWorker1.RunWorkerAsync()
backgroundWorker1 n'est pas libre

merci

2 réponses

NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
27 janv. 2014 à 12:58
Bonjour,

Pour annuler (ou stopper) un background worker, il faudrait déjà qu'il tourne.
Dans ton cas, tu fais juste un test (If backgroundWorker1.CancellationPending Then) puis tu sors de ton thread.
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
28 janv. 2014 à 21:58
Bonjour

en fait le problème est plus complexe car les sub appeler dans
le dowork sont executé par une class
j'ai réussi à résoudre le problème avec l'aide du forum voisin
ex
dans form1
class form1
private classmath as new classtoto
Private Sub Buttonvalid_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Buttonvalid.MouseClick
If Not backgroundWorker1.IsBusy Then
backgroundWorker1.RunWorkerAsync()
End If
end sub
Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork

If backgroundWorker1.CancellationPending Then
e.Cancel = True
End If
classmath.dosomething()

end sub

class toto

public sub dosomething()
'une boucle
while
if form1.backgroundWorker1.CancellationPending then
exit sub
end if
end while

end sub

end class

le gros problème et la surprise est quand la ligne
if form1.backgroundWorker1.CancellationPending dans la class
est exécuté une nouvelle instance de form1 est créer ou soit
un autre thread de form1

la solution passer en parametre la fonction .backgroundWorker1.CancellationPending

Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork
If backgroundWorker1.CancellationPending Then
e.Cancel = True
End If
classmath.dosomething(Function() backgroundWorker1.CancellationPending)

end sub

et dans la classtoto
public sub dosomething(ByVal isCancellationPending As Func(Of Boolean) )
while
if is CancellationPending then
exit sub
end if
end while


end sub
0
Rejoignez-nous