Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1.) I would recommend using the BackgroundWorker instead of separate threads like this. Your worker will catch exceptions and pass them along as a parameter to the complete handler.</p> <p>2.) I would use ShowDialog() instead of Show() when displaying the second form, this will block the DoRun() at that method call and exceptions should then be caught by your surrounding try / catch (or the BackgroundWorker if you're using that instead). </p> <p>I think the problem comes that since you're calling Show() you're essentially dispatching that call onto the Invoker, which ends up being queued in the UI thread. So when an exception happens there is nothing higher up the callstack to catch it. I believe calling ShowDialog() will fix this (and also allow you to drop that nasty for loop).</p> <p>Something like this:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // NOTE: I forget the event / method names, these are probably a little wrong. BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (o, e) =&gt; { Form2 f = new Form2(); e.Result = f.ShowDialog(); }; worker.DoWorkComplete += (o, e) =&gt; { if(e.Error != null) MessageBox.Show(string.Format("Caught Error: {0}", ex.Message)); // else success! // use e.Result to figure out the dialog closed result. }; worker.DoWorkAsync(); } } </code></pre> <p>Actually, now that I think about it, it's sort of weird to be opening a dialog from a background thread but I think this will still work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload