Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginInvoke is blocking the UI whereas Invoke is not.!
    primarykey
    data
    text
    <p>I am confused with scenario which I have encountered with cross thread access. Here is what I am trying to do:</p> <p>Main UI thread - menu item click I create a background worker and run it asynchronously</p> <pre><code>private void actionSubMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem itemSelected = (ToolStripMenuItem)sender; ExecuteTheActionSelected(itemSelected.Text); } </code></pre> <p>The method <code>ExecuteTheActionSelected</code> is as follows:</p> <pre><code>private void ExecuteTheActionSelected(string actionSelected) { BackgroundWorker localBackgroundWorker = new BackgroundWorker(); localBackgroundWorker.DoWork += new DoWorkEventHandler(localBackgroundWorker_DoWork); localBackgroundWorker.RunWorkerAsync(SynchronizationContext.Current); } </code></pre> <p>The <code>localBackgroundWorker_DoWork</code> has:</p> <pre><code> ActionExecutionHelper actionExecutioner = new ActionExecutionHelper() actionExecutioner.Execute(); </code></pre> <p>The <code>Execute</code> method in that class that has method invoker which infact invokes the event handler in UI thread:</p> <pre><code> public void Execute() { // ---- CODE ----- new MethodInvoker(ReadStdOut).BeginInvoke(null, null); } protected virtual void ReadStdOut() { string str; while ((str = executionProcess.StandardOutput.ReadLine()) != null) { object sender = new object(); DataReceivedEventArgs e = new DataReceivedEventArgs(str); outputDataReceived.Invoke(sender, e); //This delegate invokes UI event handler } } </code></pre> <p>The UI event handler is as follows:</p> <pre><code>private void executionProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (_dwExecuteAction != null) { _dwExecuteAction.ShowDataInExecutionWindow(e.Text); } } </code></pre> <p><strong>Now here comes the cross thread issue</strong>:</p> <pre><code>public void ShowDataInExecutionWindow(string message) { if (rchtxtExecutionResults.InvokeRequired) { rchtxtExecutionResults.Invoke(new ShowDataExecutionDelegate(ShowDataInExecutionWindow), message); } else { this.rchtxtExecutionResults.AppendText(message + Environment.NewLine); } } </code></pre> <p>Here <strong>Invoke doesn't block the UI where as BeginInvoke blocks</strong>. Please help me understand this scenario as i m confused a lot.</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.
 

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