Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The call stack problem is a known issue with <code>Control.Invoke</code>. You lose the call stack. Sorry. This is because it is rethrown on the UI thread using <code>throw ex;</code>.</p> <p>The best solution would be to replace the background thread with a background <code>Task</code>. Note: this solution is only available for .NET 4.0. The <code>Task</code> class properly marshals exceptions. I wrote <a href="http://nitoprograms.blogspot.com/2010/06/reporting-progress-from-tasks.html" rel="nofollow noreferrer">a blog entry</a> about reporting progress from tasks, and the code in that blog entry will allow you to catch any UI update errors in the background thread, preserving the original exception and its call stack.</p> <p>If you can't upgrade to .NET 4.0 yet, there is a workaround. <a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" rel="nofollow noreferrer">Microsoft's Rx library</a> includes a CoreEx.dll which has an extension method for <code>Exception</code> called <code>PrepareForRethrow</code>. This is supported in .NET 3.5 SP1 and .NET 4.0 (and SL 3 and SL 4). You'll need to wrap your UI updater method with something a little uglier:</p> <pre><code>private delegate void AddMessageToConsole_DELEGATE (frmMainPresenter.PresenterMessages message); private void AddMessageToConsole (frmMainPresenter.PresenterMessages message) { if (InvokeRequired) { // Invoke the target method, capturing the exception. Exception ex = null; Invoke((MethodInvoker)() =&gt; { try { AddMessageToConsole(message); } catch (Exception error) { ex = error; } }); // Handle error if it was thrown if (ex != null) { MSASession.ErrorLogger.Log(ex); // Rethrow, preserving exception stack throw ex.PrepareForRethrow(); } } else { string message_text = ""; //Message that will be displayed in the Console / written in the Log. try { message_text = I18N.GetTranslatedText(message) } catch (Exception ex) { throw new Exception(ex.Message, ex); } txtConsole.AppendText(message_text); } } </code></pre> <p>Note: I recommend you start a migration away from <code>ISynchronizeInvoke</code>. It is an outdated interface that is not carried forward into newer UI frameworks (e.g., WPF, Silverlight). The replacement is <code>SynchronizationContext</code>, which supports WinForms, WPF, Silverlight, ASP.NET, etc. <code>SynchronizationContext</code> is much more suitable as an abstract "thread context" for a business layer.</p>
 

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