[VB.net] backgroundworker targetexception
Bonjour,
Voila j'ai une application en vb.net qui fonctionne tres bien sans backgroundworker sauf sur le traitement final qui est tres gourmand et j'ai donc un gros freeze de l'interface.
Je me suis donc tourné vers le backgroundworker mais j'ai quelques difficultés.
J'ai fait mon code grâce a un tuto du site, mon code ressemble bien a celui proposé comme exemple mais j'ai quand meme l'erreur suivante lors de l'execution du traitement :
Citation:
TargetInvocationException was unhandled
Une exception a été levée par la cible d'un appel.
Et pour voila le code qui declare le backgroundworker
Code:
1 2 3 4 5 6 7
| public bgw as BackgroundWorker
bgw = New BackgroundWorker
bgw.WorkerReportsProgress = True
bgw.WorkerSupportsCancellation = True
AddHandler bgw.DoWork, AddressOf bgw_DoWork
AddHandler bgw.RunWorkerCompleted, AddressOf bgw_RunWorkerCompleted
AddHandler bgw.ProgressChanged, AddressOf bgw_ProgressChanged |
Et les methodes
DoWork
Code:
1 2 3 4 5 6
| Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
rapp_pdf.Visible = True
rapp_pdf.Enabled = True
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
creation_rapport(worker, e)
End Sub |
ProcessChanged
Code:
1 2 3
| Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles bgw.ProgressChanged
rapp_pdf.pgb_rapport.Value = e.ProgressPercentage
End Sub |
et RunWorkerCompleted
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
rapp_pdf.Visible = False
rapp_pdf.pgb_rapport.Value = 0
If rapport Then
MsgBox("Le rapport a bien été créé.", , "Félicitation") 'affiche si la creation a bien été effectuée
Me.Focus()
Else
MsgBox("Il y a eu un problème lors de la création du rapport, veuillez réessayer.", MsgBoxStyle.Exclamation & MsgBoxStyle.OkOnly, "/!\ Attention /!\")
btnrapport.Visible = True
btnrapport.Focus()
End If
bgw.CancelAsync()
End Sub |
et un extrait de creation_rapport() (qui fonctionne tres bien sans backgroundworker a part le petit freeze)
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
| 'chemin du fichier
appXls.Workbooks.Open(cheminautre & ".xls") 'ouverture du fichier excel
sheetXls = appXls.ActiveWorkbook.Worksheets("Rapport")
'valorisation des cellules
worker.ReportProgress(10)
With sheetXls
.Range("P2").Value = txtdossier.Text
.Range("P3").Value = txtmission.Text
.Range("F6").Value = txtrapportautre.Text
.Range("D15").Value = txtdepartement.Text
.Range("C16").Value = txtcommune.Text
.Range("C17").Value = txtadresse.Text
.Range("B22").Value = txtlotautre.Text
.Range("C20").Value = txtlocaautre.Text
.Range("I20").Value = txtapptautre.Text
.Range("I21").Value = txtnivautre.Text
.Range("E23").Value = txtconstrautre.Text
.Range("E24").Value = txtinstalautre.Text
.Range("E26").Value = txtvisiteautre.Text
.Range("A270").Value = txtautreconst.Text
.Range("D29").Value = txttechnicien.Text
.Range("A273").Value = txtautreconst.Text
'controle de l'alimentation via la checkbox du formulaire
worker.ReportProgress(25)
bdd_xls_pdf(chkalimautre.Checked)
'controle de la presence de constatations dans le formulaire
If txtautreconst.Text = "" Or txtautreconst.Text = "Aucune" Then
.Range("A76").Value = non
.Range("A44").Value = non
Else
.Range("A76").Value = oui
.Range("A44").Value = oui
End If
'affichage des anomalies detectées par l'agent
worker.ReportProgress(60)
show_ano()
'affichage des pieces non visitées et raison
worker.ReportProgress(85)
show_impossibilite()
End With
acr = New ACRODISTXLib.PdfDistiller
'appel de la methode impression_pdf et attribution du chemin du fichier sans son extension
worker.ReportProgress(100)
impression_pdf(cheminautre) |
Et j'ai failli oublier de vous dire que l'erreur n'indique aucune ligne dans mon code mais qu'elle survient lors de l'appel de creation_rapport()
mise a jour : au vue des tests que j'effectue j'ai trouvé que l'erreur etait au niveau du passage de parametre pour la progressbarre.
Dans l'attente de vos reponses
Cordialement
Nasty