質問編集履歴

1

実際に問題が発生しているソースを追記しました。

2018/10/30 04:56

投稿

shun_kuwa
shun_kuwa

スコア187

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- ```Program.cs
25
+ ```C#
26
26
 
27
27
  using System;
28
28
 
@@ -171,3 +171,229 @@
171
171
 
172
172
 
173
173
  情報に不足があれば追加しますのでご指摘ください。どうかよろしくお願いいたします。
174
+
175
+
176
+
177
+ ## 実際に問題が発生するコード
178
+
179
+
180
+
181
+ 以下に実際の問題(自前のMessageBoxが出力したいのに、Windowsの「ハンドルされていない例外が~」が表示されてしまう。)が発生しているソースの簡易版を載せます。
182
+
183
+ 上記の通りですが、Visual Studio上のデバッグでは想定通りの動きをしており、exeを直接実行した場合に問題が発生します。
184
+
185
+
186
+
187
+ ```C#
188
+
189
+ using System;
190
+
191
+ using System.Threading;
192
+
193
+ using System.Windows.Forms;
194
+
195
+
196
+
197
+ namespace SandboxForms
198
+
199
+ {
200
+
201
+ static class Program
202
+
203
+ {
204
+
205
+ /// <summary>
206
+
207
+ /// The main entry point for the application.
208
+
209
+ /// </summary>
210
+
211
+ [STAThread]
212
+
213
+ static void Main()
214
+
215
+ {
216
+
217
+ Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
218
+
219
+ System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program_UnhandledException);
220
+
221
+
222
+
223
+ Application.EnableVisualStyles();
224
+
225
+ Application.SetCompatibleTextRenderingDefault(false);
226
+
227
+ Application.Run(new Form1());
228
+
229
+ }
230
+
231
+
232
+
233
+ static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
234
+
235
+ {
236
+
237
+ var errorMember = e.Exception.TargetSite;
238
+
239
+ var errorMessage = e.Exception.Message;
240
+
241
+ var message = string.Format("異常が発生しました。システム管理者へ連絡してください。\r\n例外情報:例外が{0}で発生。プログラムを終了します。\r\nメッセージ:{1}",
242
+
243
+ errorMember, errorMessage);
244
+
245
+ MessageBox.Show(message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop);
246
+
247
+ }
248
+
249
+
250
+
251
+ static void Program_UnhandledException(object sender, UnhandledExceptionEventArgs e)
252
+
253
+ {
254
+
255
+ var ex = e.ExceptionObject as Exception;
256
+
257
+ if (ex == null)
258
+
259
+ {
260
+
261
+ MessageBox.Show("異常が発生しました。システム管理者へ連絡してください。\r\n例外情報なし");
262
+
263
+ }
264
+
265
+
266
+
267
+ var errorMember = ex.TargetSite.Name;
268
+
269
+ var errorMessage = ex.Message;
270
+
271
+ var message = string.Format("異常が発生しました。システム管理者へ連絡してください。\r\n例外情報:例外が{0}で発生。プログラムを終了します。\r\nメッセージ:{1}",
272
+
273
+ errorMember, errorMessage);
274
+
275
+ MessageBox.Show(message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop);
276
+
277
+ }
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ --------------------------------------------------------------------------
286
+
287
+
288
+
289
+ using System;
290
+
291
+ using System.Threading;
292
+
293
+ using System.Windows.Forms;
294
+
295
+
296
+
297
+ namespace SandboxForms
298
+
299
+ {
300
+
301
+ public partial class Form1 : Form
302
+
303
+ {
304
+
305
+ public Form1()
306
+
307
+ {
308
+
309
+ InitializeComponent();
310
+
311
+ }
312
+
313
+ private void button1_Click(object sender, EventArgs e)
314
+
315
+ {
316
+
317
+ throw new ApplicationException("例外のテスト。");
318
+
319
+ }
320
+
321
+
322
+
323
+ private void Form1_Load(object sender, EventArgs e)
324
+
325
+ {
326
+
327
+ ThreadStart ts = new ThreadStart(Form2Open);
328
+
329
+ Thread t = new Thread(ts);
330
+
331
+ t.Name = "test";
332
+
333
+ t.SetApartmentState(ApartmentState.STA);
334
+
335
+ t.Start();
336
+
337
+ }
338
+
339
+
340
+
341
+ private void Form2Open()
342
+
343
+ {
344
+
345
+ var form = new Form2();
346
+
347
+ form.ShowDialog();
348
+
349
+ }
350
+
351
+ }
352
+
353
+ }
354
+
355
+
356
+
357
+ --------------------------------------------------------------------------
358
+
359
+
360
+
361
+ using System;
362
+
363
+ using System.Windows.Forms;
364
+
365
+
366
+
367
+ namespace SandboxForms
368
+
369
+ {
370
+
371
+ public partial class Form2 : Form
372
+
373
+ {
374
+
375
+ public Form2()
376
+
377
+ {
378
+
379
+ InitializeComponent();
380
+
381
+ }
382
+
383
+
384
+
385
+ private void button1_Click(object sender, EventArgs e)
386
+
387
+ {
388
+
389
+ throw new Exception("Form2の例外");
390
+
391
+ }
392
+
393
+ }
394
+
395
+ }
396
+
397
+
398
+
399
+ ```