Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Method Invoke never returns
    primarykey
    data
    text
    <p>I've got a threaded invoke call that never returns.</p> <p>The thread runs just fine right up until I call the line that ways, "<code>owner.Invoke(methInvoker);</code>"</p> <p>When debugging, I can slowly step, step, step, but once I hit <code>owner.Invoke</code>... it's Over!</p> <pre><code>Control owner; public event ReportCeProgressDelegate ProgressChanged; public void ReportProgress(int step, object data) { if ((owner != null) &amp;&amp; (ProgressChanged != null)) { if (!CancellationPending) { ThreadEventArg e = new ThreadEventArg(step, data); if (owner.InvokeRequired) { MethodInvoker methInvoker = delegate { ProgressChanged(this, e); }; owner.Invoke(methInvoker); } else { ProgressChanged(this, e); } } else { mreReporter.Set(); mreReporter.Close(); } } } </code></pre> <p>FYI: This is a custom class that mimics the <code>BackgroundWorker</code> class, which is not available on controls that do not have Forms.</p> <p>Thinking Invoke might not be required, I manually stepped the cursor in the debugger over that part of the code and tried calling <code>ProgressChanged</code> directly, but VS2010's debugger threw a cross thread exception.</p> <p><strong>EDIT:</strong></p> <p>Due to the first 3 comments I have received, I wanted to update with my <code>ProgressChanged</code> method:</p> <pre><code>worker.ProgressChanged += delegate(object sender, ThreadEventArg e) { if (progressBar1.Style != ProgressBarStyle.Continuous) { progressBar1.Value = 0; object data = e.Data; if (data != null) { progressBar1.Maximum = 100; } progressBar1.Style = ProgressBarStyle.Continuous; } progressBar1.Value = e.ProgressPercentage; }; </code></pre> <p>There is a breakpoint on the first line of the <em>anonymous method</em>, but it never gets hit either.</p> <p><strong>EDIT 2</strong></p> <p>Here is a more complete listing of the call to the thread:</p> <pre><code>List&lt;TableData&gt; tList = CollectTablesFromForm(); if (0 &lt; tList.Count) { using (SqlCeReporter worker = new SqlCeReporter(this)) { for (int i = 0; i &lt; tList.Count; i++) { ManualResetEvent mre = new ManualResetEvent(false); worker.StartThread += SqlCeClass.SaveSqlCeDataTable; worker.ProgressChanged += delegate(object sender, ThreadEventArg e) { if (progressBar1.Style != ProgressBarStyle.Continuous) { progressBar1.Value = 0; object data = e.Data; if (data != null) { progressBar1.Maximum = 100; } progressBar1.Style = ProgressBarStyle.Continuous; } progressBar1.Value = e.ProgressPercentage; }; worker.ThreadCompleted += delegate(object sender, ThreadResultArg e) { Cursor = Cursors.Default; progressBar1.Visible = false; progressBar1.Style = ProgressBarStyle.Blocks; if (e.Error == null) { if (e.Cancelled) { MessageBox.Show(this, "Save Action was Cancelled.", "Save Table " + tList[i].TableName); } } else { MessageBox.Show(this, e.Error.Message, "Error Saving Table " + tList[i].TableName, MessageBoxButtons.OK, MessageBoxIcon.Error); } mre.Set(); }; worker.RunWorkerAsync(tList[i]); progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Visible = true; Cursor = Cursors.WaitCursor; mre.WaitOne(); } } } </code></pre> <p>I hope this isn't overkill! I hate presenting too much information, because then I get people critiquing my style. :)</p>
    singulars
    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.
 

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