Bonjour,

J'ai un projet construit ainsi :
une window contient plusieurs WindowsFormsHost (dans des onglets) qui contiennent elle même des applications form (framework 2).
A la fermeture de la window (le programme principal donc), je boucle sur les onglets, je ferme proprement les applications F2 ( ainsi si il faut enregistrer des données, une demande est faite à l'utilisateur )) et ensuite je ferme la window.
je gere aussi des applications en F3 dans les onglets.
Et souvent, en production ( jamais en test ni en dev ) j'ai l'erreur suivante :

La référence d'objet n'est pas définie à une instance d'un objet.
à System.Windows.Window.WmClose()
à System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
et je ne comprend pas pourquoi, bien entendu.

voici le code que j'utilise pour ajouter une appli F2 :
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 
 
 private void LancementAppli(string clé, object parametre)
        {
            Mouse.OverrideCursor = Cursors.Wait;
 
 
                if (Lanceur.ListesApplis.Contains(clé.ToUpper()))
                {
                    AppliLanceur Al = Lanceur.ListesApplis[clé.ToUpper()];
 
 
                    if (Al.estSouSListeApplis)
                    {
                        ImgSSMenu.Source = new BitmapImage(new Uri(Al.Image));
                        TitreSSMenu.Text = Al.Titre;
                        CleListeSousAppli = Al.Clé;
                        afficheListeSousAppli();
                    }
                    else
                    {
                        if (Al.estSousApplis)
                            afficheListeSousAppli();
                        else
                        {
                            //  CleListeSousAppli = "";
                            //  TitreSSMenu.Text = "Recherche rapide";
                            afficheRechercheRapide();
                        }
                        if (Al.estActive & !Al.PlusieursInstances)
                        {
                            if (Al.Clé.ToUpper() == CléFormulaire)
                                if (parametre != null)
                                {
                                    bool btemp = (bool)parametre;
                                    if (btemp)
                                    {
                                        LancementSelectAppliF2(CléFormulaire, false).MgaMessageSend(new MgaMessage(MgaConfig.dbData.mainApp, "formAffList", null));
                                        Mouse.OverrideCursor = null;
 
                                        // si on a choisi lanceur toujours ouvert, on ne fait rien, sinon on ferme
                                        if (!profil.lanceur_tjs_ouvert)
                                            ExpanderLanceur.IsExpanded = false;
                                        return;
                                    }
                                }
 
                            // on selectionne l'onglet de l'appli
                            foreach (CloseableTabItem Ti in OngletsApplis.Items)
                                if (Ti.Tag.ToString() == Al.Clé + "1")
                                {
                                    //Ti.IsSelected = true;
                                    Ti.IsSelected = !enTrainDeQuitter;
                                    // si c'est formulaire
                                    Mouse.OverrideCursor = null;
 
                                    // si on a choisi lanceur toujours ouvert, on ne fait rien, sinon on ferme
                                    if (!profil.lanceur_tjs_ouvert)
                                        ExpanderLanceur.IsExpanded = false;
                                    return;
                                }
                        }
                        else
                        {
 
                            bool Generation = false;
                            MangustaLib.MgaForm F2 = null;
                            mangustalibF3.mgaGrid F3 = null;
                            if (Al.Framework == 2)
                            {
                                F2 = (MangustaLib.MgaForm)Al.NouvelleInstance(parametre);
                                Generation = (F2 != null);
                            }
                            if (Al.Framework == 3)
                            {
                                F3 = (mangustalibF3.mgaGrid)Al.NouvelleInstance(parametre);
                                Generation = (F3 != null);
                            }
                            if (Generation)
                            {
                                // on rajoute un onglet
                                CloseableTabItem Ti = new CloseableTabItem();
                                //   Ti.Name = Al.Clé;
                                Ti.Visibility = Visibility.Visible;
 
                                if (Al.Framework == 2)
                                {
                                    //  on y rajoute un host
                                    WindowsFormsHost host = new WindowsFormsHost();
 
                                    host.Name = "FenetreHost_F2";
                                    Ti.Content = host;
                                    //SolidColorBrush b1 = new SolidColorBrush(Color.FromArgb(128,255,0,0));
                                    //SolidColorBrush b2 = new SolidColorBrush(Color.FromArgb(128, 0, 255, 0));
                                    //host.Background=b1;
                                    //Ti.Background=b2;
                                    host.Child = F2;
                                    host.VerticalAlignment = VerticalAlignment.Stretch;
                                    host.HorizontalAlignment = HorizontalAlignment.Stretch;
                                    //Ti.Header = F2.Text;
                                    Ti.Header = Al.Titre;
                                    F2.Tag = Al.Clé + Al.NbInstance.ToString();
                                    F2.FormClosing += new System.Windows.Forms.FormClosingEventHandler(F1_FormClosing);
                                    F2.TextChanged += new EventHandler(F1_TextChanged);
                                    //F2.DockChanged += new EventHandler(F2_DockChanged);
 
                                }
                                if (Al.Framework == 3)
                                {
 
                                    //  on y rajoute un host
                                    //Frame F = new Frame();
                                    //F.Content = F3;
                                    Ti.Content = F3;
                                    //if (F3.Titre != "")
                                    //    Ti.Header = F3.Titre;
                                    //else
                                    Ti.Header = Al.Titre;
                                    F3.Tag = Al.Clé + Al.NbInstance.ToString();
                                    // F3.onClose += new RoutedEventHandler(F3_onClose);
                                    F3.onClose += new mgaGrid.CloseEventHandler(F3_onClose);
 
                                }
 
                                Ti.Tag = Al.Clé + Al.NbInstance.ToString();
                                OngletsApplis.Items.Add(Ti);
                                //    Image Timg = (Image)Ti.FindResource("TabImage");
 
                                // if (Ti.TabImage!=null)
                                if (File.Exists(Al.Image))
                                    Ti.TabImage.Source = new BitmapImage(new Uri(Al.Image));
 
                                Ti.IsTabStop = true;
 
                                Ti.CloseTab += new RoutedEventHandler(Ti_CloseTab);
                                Lanceur.LancementAppli(Al.Clé);
 
                                //Ti.IsSelected = true;
                                if (MgaConfig.SelectionAppli)
                                {
                                    Ti.IsSelected = !enTrainDeQuitter;
                                    Ti.Focus();
                                }
                                if (Al.Framework == 2)
                                    F2.SelectNextControl(null, true, true, true, true);
                                if (Al.Framework == 3)
                                    F3.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
                            }
                            RafraichissementListeFavoris();
                        }
 
                        // si on a choisi lanceur toujours ouvert, on ne fait rien, sinon on ferme
                        if (!profil.lanceur_tjs_ouvert)
                            ExpanderLanceur.IsExpanded = false;
 
                    }
                }
                else
                    MessageBox.Show("Impossible de lancer l'application : " + clé + ", celle-ci n'est pas présente la liste...");
 
                RafraichissementListeSousAppli();
                       Mouse.OverrideCursor = null;
        }
voici le code que j'utilise pour fermet les onglets :

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
 
  void Ti_CloseTab(object sender, RoutedEventArgs e)
         {
             string temp = "";
 
             try
             {
                 CloseableTabItem tabItem = null;
                 TabControl tabControl = null;
                 try
                 {
                     tabItem = e.Source as CloseableTabItem;
                     if (tabItem != null)
                     {
                         tabItem.IsSelected = true;
                         tabControl = tabItem.Parent as TabControl;
                         if (tabControl != null)
                         {
                             try
                             {
                                 temp = CléParInstance(tabItem.Tag.ToString());
                                 if (Lanceur.ListesApplis.Contains(temp))
                                     if (Lanceur.ListesApplis[temp].estActive)
                                     {
                                         if (Lanceur.ListesApplis[temp].Framework == 2)
                                         {
                                             try
                                             {
                                                 if (tabItem.Content != null)
                                                 {
                                                     WindowsFormsHost host = (WindowsFormsHost)tabItem.Content;
                                                     if (host.Child != null)
                                                     {
                                                         MgaForm F1 = (MangustaLib.MgaForm)host.Child;
                                                         try
                                                         {
                                                             F1.Close();
                                                             if (bContinuerFermer)
                                                             {
                                                                 try
                                                                 {
                                                                     F1.Dispose();
                                                                     host.Child = null;
                                                                     host.Dispose();
                                                                 }
                                                                 catch
                                                                 { }
                                                                 try
                                                                 {
                                                                     // si l'appli est toujours considéré comme active on la stoppe
                                                                     if (temp != "")
                                                                         if (Lanceur.ListesApplis[temp].estActive)
                                                                         {
                                                                             Lanceur.ArretAppli(temp);
                                                                             Lanceur.ListesApplis[temp].NbInstance--;
                                                                         }
                                                                     // on retire le nom de l'appli courante
                                                                     if (MgaConfig.AppliCourante == "*" + temp + "*")
                                                                         MgaConfig.AppliCourante = "*" + Lanceur.ListesApplis.nbApplisLancées.ToString() + "* -> fermeture onglet (1) " + temp;
                                                                     tabItem.Content = null;
                                                                     tabControl.Items.Remove(tabItem);
                                                                 }
                                                                 catch
                                                                 {
                                                                     // pas de message 
                                                                 }
                                                             }
                                                         }
                                                         catch 
                                                         {
                                                             // on ne met pas de message sinon le message bloque et on ne peut pas fermer
                                                             // MessageBox.Show("Une erreur est survenue lors de la fermeture de l'onglet...\n"+ex.Message);
 
                                                             //host.Dispose();
                                                             try
                                                             {
                                                                 if (bContinuerFermer)
                                                                 {
                                                                     F1.Dispose();
                                                                     host.Child = null;
                                                                     // si l'appli est toujours considéré comme active on la stoppe
                                                                     if (temp != "")
                                                                         if (Lanceur.ListesApplis[temp].estActive)
                                                                         {
                                                                             Lanceur.ArretAppli(temp);
                                                                             Lanceur.ListesApplis[temp].NbInstance--;
                                                                         }
                                                                     // on retire le nom de l'appli courante
                                                                     if (MgaConfig.AppliCourante == "*" + temp + "*" || MgaConfig.AppliCourante.StartsWith("*"))
                                                                         MgaConfig.AppliCourante = "*" + Lanceur.ListesApplis.nbApplisLancées.ToString() + "* -> fermeture onglet (2) " + temp;
                                                                     tabItem.Content = null;
                                                                     tabControl.Items.Remove(tabItem);
                                                                 }
                                                             }
                                                             catch
                                                             {
                                                                 // pas de message 
                                                             }
                                                         }
                                                     }
                                                     //try
                                                     //{
                                                     //    host.Dispose();
                                                     //}
                                                     //catch
                                                     //{
                                                     //    // pas de message 
                                                     //}
                                                 }
                                             }
                                             catch
                                             {
                                                 try
                                                 {
                                                     // pas de message 
                                                     if (bContinuerFermer)
                                                     {
                                                         if (MgaConfig.AppliCourante == "*" + temp + "*" || MgaConfig.AppliCourante.StartsWith("*"))
                                                             MgaConfig.AppliCourante = "*" + Lanceur.ListesApplis.nbApplisLancées.ToString() + "* -> fermeture onglet (3) " + temp;
                                                         tabItem.Content = null;
                                                         tabControl.Items.Remove(tabItem);
                                                     }
                                                 }
                                                 catch
                                                 { }
                                             }
                                         }
                                         else
                                         {
                                             mangustalibF3.mgaGrid F2 = null;
                                             if (tabItem.Content != null)
                                             {
                                                 F2 = (mangustalibF3.mgaGrid)tabItem.Content;
                                                 if (F2 != null)
                                                 {
                                                     F2.Close();
                                                     if (bContinuerFermer)
                                                     {
                                                         try
                                                         {
                                                             F2 = null;
                                                             tabItem.Content = null;
                                                         }
                                                         catch
                                                         { }
                                                     }
                                                 }
                                             }
                                         }
                                     }
 
                             }
                             catch
                             { }
                         }
                     }
                 }
                 catch 
                 {
                     try
                     {
                        // MessageBox.Show("Une erreur est survenu lors de la fermeture d'un onglet...\n" + excep.Message);                         
                         // si l'appli est toujours considéré comme active on la stoppe
                         if (temp != "")
                             if (Lanceur.ListesApplis[temp].estActive)
                             {
                                 Lanceur.ArretAppli(temp);
                                 Lanceur.ListesApplis[temp].NbInstance--;
                             }
                         // on retire le nom de l'appli courante
                         if (MgaConfig.AppliCourante == "*" + temp + "*" || MgaConfig.AppliCourante.StartsWith("*"))
                             MgaConfig.AppliCourante = "*" + Lanceur.ListesApplis.nbApplisLancées.ToString() + "* -> fermeture onglet (4) " + temp;
                         tabItem.Content = null;
                         tabControl.Items.Remove(tabItem);
                     }
                     catch
                     {
                         // pas de message 
                     }
                 }
             }
             catch 
             {
                 MgaConfig.AppliCourante = "*" + Lanceur.ListesApplis.nbApplisLancées.ToString() + "* -> fermeture onglet (5) " + temp;
                 // pas de message
                // MessageBox.Show("Une erreur est survenu lors de la fermeture d'un onglet...\n" + excep.Message);
             }
         }
le code pour fermer les applis F2

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
 void F1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            try
            {
                // on se refere au tag !
                MangustaLib.MgaForm F1 = (MangustaLib.MgaForm)sender;
                if (!e.Cancel)
                {
                    CloseableTabItem Tferme = null;
                    foreach (CloseableTabItem Ti in OngletsApplis.Items)
                        if (Ti.Tag.ToString() == F1.Tag.ToString())
                            Tferme = Ti;
                    if (Tferme != null)
                    {
                        OngletsApplis.Items.Remove(Tferme);
                        string temp = CléParInstance(F1.Tag.ToString());
                        Lanceur.ArretAppli(temp);
                        Lanceur.ListesApplis[temp].NbInstance--;
                        return;
                    }
                }
                else
                {
                    e.Cancel = true;
                    bContinuerFermer = false;
                }
            }
            catch 
            {
              //  MessageBox.Show("Une erreur est survenue lors de la fermeture de module...\n" + ex.Message);
            }
        }
le code pour fermer les applis F3

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
void F3_onClose(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                // on se refere au tag !
                mangustalibF3.mgaGrid F1 = (mangustalibF3.mgaGrid)sender;
                if (!e.Cancel)
                {
                    CloseableTabItem Tferme = null;
                    foreach (CloseableTabItem Ti in OngletsApplis.Items)
                        if (Ti.Tag.ToString() == F1.Tag.ToString())
                            Tferme = Ti;
                    if (Tferme != null)
                    {
                        OngletsApplis.Items.Remove(Tferme);
                        string temp = CléParInstance(F1.Tag.ToString());
                        Lanceur.ArretAppli(temp);
                        Lanceur.ListesApplis[temp].NbInstance--;
                        return;
                    }
                }
                else
                {
                    e.Cancel = true;
                    bContinuerFermer = false;
                }
            }
            catch 
            {
              //  MessageBox.Show("Une erreur est survenue lors de la fermeture de module...\n" + ex.Message);
            }
        }
et enfin le code pour fermer la window :

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  void Window2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (!QuitteMGALogin)
                {
                    //try
                    //{
                        Properties.Settings.Default.NomFichierVersion = MgaConfig.NumVersionFichier;
                        Properties.Settings.Default.Save();
 
                        if (MessageBox.Show("Voulez-vous quitter Mangusta ?", "Fermeture", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            profil.Save();
                            bContinuerFermer = true;
                            enTrainDeQuitter = true;
                            while (bContinuerFermer & OngletsApplis.Items.Count > 0)
                            {
                                    CloseableTabItem Ti = (CloseableTabItem)OngletsApplis.Items[0];
                                    Ti.IsSelected = true;
                                    OngletsApplis.SelectedIndex = 0;
                                    Ti.Close();
                            }
                            enTrainDeQuitter = e.Cancel;
                            #region toute fin de MGA
                            if (bContinuerFermer)
                            {
                                try
                                {
                                    try
                                    {
                                        //System.Threading.Thread.Sleep(2500); // x en ms 
                                        Fonctions.kill_AdminUpdateChecker();
                                    }
                                    catch
                                    {
 
                                    }
                                    try
                                    {
                                        //System.Threading.Thread.Sleep(250); // x en ms 
                                        Fonctions.EstUtilisateurConnecté(false);
                                    }
                                    catch
                                    {
 
                                    }
                                    try
                                    {
                                        //System.Threading.Thread.Sleep(250); // x en ms 
                                        Fonctions.CreationFichierRelanceLiveUpdate();
                                    }
                                    catch
                                    {
 
                                    }
                                    try
                                    {
                                        //System.Threading.Thread.Sleep(50); // x en ms 
                                        Fonctions.LanceJurisTexte();
                                    }
                                    catch
                                    {
 
                                    }
                                }
                                catch
                                {
 
                                }
                            }
                            #endregion
                            e.Cancel = !bContinuerFermer;
                        }
                        else
                            e.Cancel = true;
                    //}
                    //catch (Exception excep)
                    //{
                    //    MessageBox.Show("Une erreur est survenue lors de la fermeture de Mangusta...\n" + excep.Message);
                    //}
                }
            }
            catch (Exception excep)
            {
                MessageBox.Show("Une erreur est survenue lors de la fermeture de Mangusta...\n" + excep.Message + "\n"+excep.StackTrace);
            }
        }
j'ai rajouter des tas de try catch ( ce qui doit pas être super propre ni performant ) mais rien n'y fait...

merci d'avance.
Dimitri

edit : zut je l'ai pas mis au bon endroit. Désolé !