[RESOLVED] [2005] Cross-thread opCross-thread operation not valid. Please help!
Hi,
I'm not explicitly using threading, but I'm getting this error:
Cross-thread operation not valid: Control 'lvStaticGrps' accessed from a thread other than the thread it was created on.
I've just realised why threads are involved: I'm using a FileSystemWatcher to detect when a file is saved. The program responds by loading data from the newly saved file and updating a list view. It's while accessing the list view that the above error occurs.
I've read up a little about threading, but I really have no idea. Do i have to use a Delegate sub and Invoke? Eeck.
Thx for your help!
Re: [2005] Cross-thread opCross-thread operation not valid. Please help!
If you set the FSW's SynchronizingObject property to a control then it will raise its events in the thread that owns that control. When you add a FSW in the designer it will use the form as the SynchronizingObject by default. If it's your intention to access controls from the FSW's event handlers then setting the SynchronizingObject property is almost certainly preferable, so you avoid the need for delegation.
Note that the System.Timers.Timer has a similar property.
Re: [2005] Cross-thread opCross-thread operation not valid. Please help!
Setting the synch object to the form did it. Thx!
Code:
With fsw
.SynchronizingObject = Me
.EnableRaisingEvents = True
.NotifyFilter = NotifyFilters.LastWrite
End With
Re: [RESOLVED] [2005] Cross-thread opCross-thread operation not valid. Please help!
Man I just spent 2 hours getting delegation stuff to work. Great solution!