1
TabControl _tabControl;
TabPage _tabPage;
ListView _listView;

///
this._tabControl.Controls.Add(this._tabPage);
this._tabPage.Controls.Add(this._listView)
///

private void UpdateListView()
{
    If (_listView.InvokeRequired)
    {
        IAsyncResult _IAsyncResult = _listView.BeginInvoke(//...invoke method and parameters...//);

        While (!_IAsyncResult.IsCompleted) { Application.DoEvents(); }

        _listView.EndInvoke(_IAsyncResult);
    }
    else
        _listView.Items[0].SubItems[0].Text = "[some string]";
}

i'm calling UpdateListView() from a thread other than the UI thread. I got cross thread exception in the statement "_listView.BeginInvoke(//...//);" and the exception is raised for _tabControl.

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=Cross-thread operation not valid: Control '_tabControl' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
       at System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
       at TestApp.frmMain_Sub.UpdateListView(ListView _lv, Int32 _itemIndex, Int32 _subItemIndex, String _txt) in C:\Test\TestApp\frmMain_Sub.cs:line 5536
       at TestApp.frmMain_Sub.UpdateStatus() in C:\Test\TestApp\frmMain_Sub.cs:line 5012
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

how should I handle this scenario ? Very much appreciate who could walk me through this.

emerssso
  • 2,376
  • 18
  • 24
Drake
  • 11
  • 2
  • What language/platform/framework is this? Java? Also, please share the full stack trace/message of your exception. – emerssso Mar 19 '18 at 23:32
  • thank for replying. I'm using C# .NET Framework 4 – Drake Mar 20 '18 at 00:44
  • You probably want to edit your question and add this to it, rather than in comments (they get lost). – emerssso Mar 20 '18 at 02:34
  • thanks for your suggestion, I have edited in my question together with the code snippet . – Drake Mar 20 '18 at 14:51
  • It is the kind of crash you'd get when you created forms on more than one thread. Never ever do this. – Hans Passant Mar 20 '18 at 15:08
  • Hi Hans, if that so, any recommendation for me create forms ? technically now I have one computer and I have 5 monitors to display, I have one main form and one sub main form class and I create 5 individual threads in main form to display the sub main forms for 5 different monitors, they should work independently ... appreciate your advise. Thanks – Drake Mar 23 '18 at 21:40

0 Answers0