Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3804822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:34:48+00:00 2026-05-19T14:34:48+00:00

VB.NET 2010, .NET 4 Hello all, My question is, imagine I have two List(Of

  • 0

VB.NET 2010, .NET 4

Hello all,

My question is, imagine I have two List(Of T) objects and a subroutine in a multithreaded environment that modifies both of these objects. I don’t understand locks very well, so I’m unsure whether I can just do:

SyncLock CType(List1, IList).SyncRoot

  List1.Clear()
  List2.Clear()

End SyncLock

or must I:

SyncLock CType(List1, IList).SyncRoot
  SyncLock CType(List2, IList).SyncRoot

    List1.Clear()
    List2.Clear()

  End SyncLock
End SyncLock

? Any insight? Am I even on the right track? Any comments will be appreciated.

Thanks a lot in advance, Brian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:34:48+00:00Added an answer on May 19, 2026 at 2:34 pm

    First, it is bad practice to lock on a non-private object, as something else might lock on it, and then things go downhill from there, instead lock on a private member object, something like

    Class Class1
        Private lockObject as New Object
    
        Public Sub Clear()
            SyncLock lockObject
               ...
            End Synclock
        End Sub
    End Class
    

    Now, for the actual question: Unless every operation you do modifies both lists (doubtful), you should have one lock per list. While possible to use one lock object to mean “I’m doing something with a list”, it doesn’t make much sense to block a method in another thread that isn’t operating on the same list as yours, and will just slow down everything.

    So, in short: Use one lock object per set of locks (Locks for operating on list1, locks for list 2, etc). It’s slightly more code for sections that operate on both lists, but the code will be more performant.

    Also, as a general note: Hold the lock for as little time as possible. The less time you spent in a lock, the less chance another thread will come along and be blocked until you’re done.

    The MSDN Page on SyncLock might also be worth a read, it contains this information and a few examples. And @BasicLife is correct, always make sure to take the locks in the same order everywhere.

    And a general rule throughout the framework, unless you are accessing a static member on a class, unless otherwise stated, it is not thread safe. So when operating on lists, you will want to lock when you add, remove, clear or enumerate over the list, I’m sure there’s others, but those are the most common.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VB.NET 2010, .NET 4 Hello all, I have a System.Timers.Timer object that does some
how can I list all checked out items in vs.net 2010 tfs? where in
VB.NET 2010, .NET 4 Hello all, I'm by no means a good programmer and
VB.NET 2010, .NET 4 Hello, I recently read about using SynchronizationContext objects to control
Question: Assume a C++ hello world program, non .NET. With Visual Studio 2005/2008/2010, how
I am using vb.net 2010 and I have created a program that uses sockets
I am using VB.net 2010 and I have two projects: SQLtesting and Controls .
Has anyone migrated a VB6 project to .Net with Visual Studio 2010? I have
Is there a plugin to VS.NET 2010 that implements kinetic scrolling like on the
I am using vb.net 2010. I have a datagridview whose data source is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.