Bonjour,
Voilà, je m'arrache les cheveaux avec les background worker !, pour faire simple : j'ai une application vb.net avec 2 boutons (btn_test1, btn_test2), un BackgroundWorker (bg_worker) et une strucutre (mystruct), je vous invite dans un premier temps a regarder le code rapidement :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#Dans un module
 
    Public Structure My_Struct
        Public id, sql, txt_progress As String
        Public btn_to_active, result As Object
    End Structure
 
#fin du module
 
#Dans mon Winform
 
 Private Sub btn_test1_Click(...)..
   dim my_stc As New My_Struct
 
   btn_test2.enabled = false
   With my_stc
   .id = "toto"
   .sql = "Select ... FROM ..."
   .txt_progress= "do progress"
   .btn_to_active = btn_test2
   End With
   bg_worker.RunWorkerAsync(my_stc)
 
End Sub
 
 Private Sub btn_test2_Click(...)..
   dim my_stc As New My_Struct
 
   btn_test4.enabled = false
   With my_stc
   .id = "titi" 'RMARQUER LE CHANGEMENT DE ID
   .sql = "Select ... FROM ..."
   .txt_progress= "do progress"
   .btn_to_active = btn_test1 ''changement
   End With
   bg_worker.RunWorkerAsync(my_stc)
End Sub
 
 
    Private Sub bg_worker_DoWork(...) Handles bg_worker.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Select Case e.Argument.id
            Case "toto"
                e.Result = do_traitement_toto(e.Argument, worker) ' return un type My_Struct
            Case "titi"
                e.Result = do_traitement_toto(e.Argument, worker) ' return unb type  My_Struct
        End Select
    End Sub
 
     Private Sub bg_worker_RunWorkerCompleted(...) Handles bg_worker.RunWorkerCompleted
        Select Case e.Result.id
            Case "toto"
             messagebox.show("toto")
             e.Result.btn_to_active.enabled = true
            Case "titi" 
             messagebox.show("titi")
             e.Result.btn_to_active.enabled = true
         End Select
    End Sub
Quand je clique sur le boutton "btn_test1", tout marche nickel et j'ai le message box avec "toto" par contre quand je clique sur bouton "btn_test2" j'ai ce message d'erreur "Une exception a été levée par la cible d'un appel." avec {"Variable objet ou variable d'un bloc With non définie."}

Est ce que quelqu'un à une idée de ce qui pose problème ??

Merci de votre aide