Connecting Tech Pros Worldwide Help | Site Map

Cross-thread operation not valid: Control 'listView2' accessed from a thread other th

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 9 '09
error message


System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'listView2' 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.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
at System.Windows.Forms.ListView.InsertItemsNative(In t32 index, ListViewItem[] items)
at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex, ListViewItem[] items, Boolean checkHosting)
at System.Windows.Forms.ListView.ListViewNativeItemCo llection.Add(ListViewItem value)
at System.Windows.Forms.ListView.ListViewItemCollecti on.Add(ListViewItem value)
at System.Windows.Forms.ListView.ListViewItemCollecti on.Add(String text, Int32 imageIndex)
at System.Windows.Forms.ListView.ListViewItemCollecti on.Add(String text)
at test.MainForm.DataArrival(Object sender, PacketArrivedEventArgs e) in D:\bk\bk1\projects asp\nids\nids project\Project\nids\NIDSSUBVER\Sniffing.cs:line 289
at test.RawSocket.OnPacketArrival(PacketArrivedEventA rgs e) in D:\bk\bk1\projects asp\nids\nids project\Project\nids\NIDSSUBVER\RawSocket.cs:line 263
at test.RawSocket.Receive(Byte[] buf, Int32 len) in D:\bk\bk1\projects asp\nids\nids project\Project\nids\NIDSSUBVER\RawSocket.cs:line 144
at test.RawSocket.CallReceive(IAsyncResult ar) in D:\bk\bk1\projects asp\nids\nids project\Project\nids\NIDSSUBVER\RawSocket.cs:line 156
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.ContextAwareResult.CompleteCallback(Obj ect state)
at System.Threading.ExecutionContext.runTryCode(Objec t userData)
at System.Runtime.CompilerServices.RuntimeHelpers.Exe cuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(Exec utionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback (Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.Compl etionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCo mpletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Oct 9 '09

re: Cross-thread operation not valid: Control 'listView2' accessed from a thread other th


DId you want to ask a question?

But the error message is pretty much self explanitory.
You created a control on one thread. then tried to directly access/modify it from another. You can't do that.

You need to use Invoke to access the control.

For example
Expand|Select|Wrap|Line Numbers
  1.                             this.Invoke((MethodInvoker)delegate
  2.                             {
  3.                                 pnlDisplay.Controls.Add(MyPhotos[Index]);
  4.                                 MyPhotos[Index].Load();
  5.                             });
  6.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#3: Oct 9 '09

re: Cross-thread operation not valid: Control 'listView2' accessed from a thread other th


That exception is pretty much ONLY generated in debug mode (otherwise its ignored and not fired), it also comes with a nice little link to an MSDN article IN the exception popup about how to handle it. Plenty of good examples there, although the prefered is tlhintoq's method
Reply