Results 1 to 4 of 4

Thread: Lock

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Lock

    Hey,

    I have a class (static) with a static collection property (public).
    I have a background worker which iterates through the collection and assigns some properties (it is quite time consuming).

    There is also a refresh method (public) which can be called to refresh the collection.

    The problem is this:
    While the background worker is working iterating through the collection, the refresh method is called from somewhere else resetting the collection and causing the background worker to generate an error.

    My question is this:
    Is there a way to Lock the collection so that it cannot be modified while the background worker is working on it?
    I know I can use Lock(object) but that locks some lines of code. is there a way to lock a variable?

    Thanks
    Don't anthropomorphize computers -- they hate it

  2. #2
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Lock

    This might not be what you're looking for but you could have a boolean that says if its accessible or not. Start it when the background worker starts and change the value when the worker is finished.
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Lock

    http://msdn.microsoft.com/en-us/libr...1t(VS.71).aspx
    Synclock

    Place the code that modifies the collection in a single method which is used by all other methods.

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

    Re: Lock

    Quote Originally Posted by mendhak
    http://msdn.microsoft.com/en-us/libr...1t(VS.71).aspx
    Synclock

    Place the code that modifies the collection in a single method which is used by all other methods.
    SyncLock is the VB version. In C# it's the 'lock' key word. Both are language features that are implemented using, I believe, a Monitor.
    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

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