Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Cross-thread opCross-thread operation not valid. Please help!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Resolved [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    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

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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!
    VB 2005, Win Xp Pro sp2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width