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

  • SEARCH
  • Home
  • 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 818465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:09:26+00:00 2026-05-15T02:09:26+00:00

vb.Net multithreading question: What is the difference between SyncLock syncRoot ”# Do Stuff End

  • 0

vb.Net multithreading question:

What is the difference between

SyncLock syncRoot  
  ''# Do Stuff  
End SyncLock

-and-

SyncLock Me  
  ''# Do Stuff  
End SyncLock
  • 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-15T02:09:27+00:00Added an answer on May 15, 2026 at 2:09 am

    All code that happens within a SyncLock block is synchronized with all other code happening within a SyncLock block on the same object. Obviously, Me is not the same as syncRoot (which is, I’m assuming, Me.SyncRoot, if your Me is an ICollection).

    Code happening within a SyncLock block on one object is not going to be synchronized with code within a SyncLock block on a different object.

    Say you have this code:

    ' happening on thread 1 '
    SyncLock myColl.SyncRoot
        myColl.Add(myObject)
    End SyncLock
    
    ' happening on thread 2 '
    SyncLock myColl.SyncRoot
        myColl.Remove(myObject)
    End SyncLock
    

    The above is fine: the Add and Remove calls are synchronized, meaning they will not occur simultaneously (whichever gets called first will execute, and the second will not execute until the first is finished).

    But suppose you had this instead:

    ' happening on thread 1 '
    SyncLock myColl.SyncRoot
        myColl.Add(myObject)
    End SyncLock
    
    ' happening on thread 2 '
    SyncLock myColl ' NOTE: SyncLock on a different object '
        myColl.Remove(myObject)
    End SyncLock
    

    The above Add and Remove calls are not synchronized in any way, shape, or form. Thus there is no thread safety in the above code.

    Now, why does SyncRoot exist? Quite simply, because it makes sense to synchronize on the smallest scale necessary; i.e., there is no need to synchronize code that doesn’t actually need to be synchronized.

    Consider this example:

    ' happening on thread 1 '
    SyncLock myColl
        myColl.Add(myObject)
    End SyncLock
    
    ' happening on thread 2 '
    SyncLock myColl
        ' Why you would have code like this, I do not know; '
        ' this is just for illustration. '
        myColl.Name = myColl.Name.Replace("Joe", "Bill")
    End SyncLock
    
    ' happening on thread 3 '
    SyncLock myColl
        myColl.Name = myColl.Name.Replace("Bill", "Joe")
    End SyncLock
    

    In the above, you are synchronizing more than necessary. The Add call really has nothing to do with the renaming of the myColl object; thus the code does not need to be synchronized.

    This is the idea behind the SyncRoot property: it gives you an object whose entire purpose is to provide a common object with which modifications to/enumerations of the collection can be synchronized. Code that involves the collection in some other way — but which does not need to be synchronized with code that modifies or reads the contents of the collection — should be synchronized, where appropriate, on a different object.

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

Sidebar

Related Questions

Can any one share some study material for multithreading on .net? EDIT: I don't
Can somebody point me to a good site/book/article about multithreading with .net? I didn't
We have a vb.net application where we have implemented multithreading. We determined that we
I'm currently using Retlang for message-based multithreading in .NET, which is a great library.
I was asked this question in a .net/C# interview: If we have two threads
I'm writing a performance-critical .NET application which makes heavy use of multithreading. Using the
Does anyone have any books written for .net that deal with multithreading? I've looked
Sorry, I am very new to all this multithreading stuff... I'm working on a
My hobby is writing up stuff on a personal wiki site: http://comp-arch.net . Currently
I want to start reading about multithreading and parallel computing in C# / .Net

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.