Bonjour,
Je suis sur un problème que je ne parviens pas à résoudre.
Cela doit venir peut être d'un problème de design...
Simplement, j'ai reproduis mon scénario dans un petit projet test.
Le contexte est le suivant :
J'ai une thread qui en lance une seconde. La seconde plante...
J'aimerais traiter l'erreur, et interrompre la thread en cours...
J'arrive à récupérer l'erreur levée par la thread enfant dans l'event UnhandledException de l'AppDomain. Le problème, c'est que malgré le fait que je traite l'erreur dans cette event, j'ai la thread enfant qui se relance, et qui me relance à l'infini mon exception...
Je ne comprends pas pourquoi il me relance constamment cette thread...
Si quelqu'un peut m'éclairer sur le pourquoi et sur le comment résoudre ce problème.
Merci !
Mon app.cs :
Ma classe principale :
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 using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; using System.Runtime.ExceptionServices; namespace HandledErrorMultiThread { /// <summary> /// Logique d'interaction pour App.xaml /// </summary> public partial class App : Application { public App() : base() { // Principal thread this.Dispatcher.UnhandledException += OnDispatcherUnhandledException; // Child thread AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); currentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(currentDomain_FirstChanceException); } /// <summary> /// Called when [dispatcher unhandled exception]. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param> void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { MessageBox.Show(e.Exception.Message); e.Handled = true; } static void MyHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception)args.ExceptionObject; MessageBox.Show(e.Message); } static void MyHandler2(object sender, UnhandledExceptionEventArgs args) { // Exception e = (Exception)args.ExceptionObject; // Tools.ErrorsTools.ShowErrorMessage(e); // //sender.Han= true; } static void currentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs args) { Exception e = args.Exception; MessageBox.Show(e.Message); } } }
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; namespace HandledErrorMultiThread { /// <summary> /// Logique d'interaction pour MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Thread mainThread = new Thread(new ThreadStart(MainThreadJob)); mainThread.SetApartmentState(ApartmentState.STA); mainThread.IsBackground = false; mainThread.Start(); } public void MainThreadJob() { MessageBox.Show("Start mainJob !"); Thread childThread = new Thread(new ThreadStart(ChildThreadJob)); childThread.SetApartmentState(ApartmentState.STA); childThread.IsBackground = false; childThread.Start(); } public void ChildThreadJob() { MessageBox.Show("Start childJob !"); throw new Exception(); } } }
Partager