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 8631717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:22+00:00 2026-06-12T09:15:22+00:00

Can someone help me understand how synclock statements work? I know there are some

  • 0

Can someone help me understand how synclock statements work? I know there are some other posts about synclock here but I was not able to follow the answers very well. I am trying to get a simple multithreading example up and running but am having trouble. At first I tried the following code.

Public Class Class1
    Public list As List(Of Integer) = New List(Of Integer)

    Public Sub addInt(ByVal i As Integer)
        SyncLock list
            list.Add(i)
        End SyncLock
    End Sub

    Public Sub MainLoop()
        While list.Count < 50
            SyncLock list
                For Each i As Integer In list
                    Debug.WriteLine(i.ToString())
                Next
            End SyncLock
        End While
    End Sub
End Class

I had a simple winform with two buttons. I used the first button to create a obj of Class1 and start the MainLoop method in a new thread. And I used the seconded button to call the addInt method. However the code might work for a press or two and then lock up. After reading the other questions from this form I realized that the lockobject for the synclock statement did not work as I initially thought, and was just an arbitrary reference object that should not ever be changed. I think the syncLock statement just forces the whole code block to execute before passing processor control to a new thread. So I tried this using another lock object but now it just locks up.

Public Class Class1
    Private padLock As String = "PADLOCK"
    Public list As List(Of Integer) = New List(Of Integer)

    Public Sub addInt(ByVal i As Integer)
        SyncLock padLock
            list.Add(i)
        End SyncLock

    End Sub

    Public Sub MainLoop()
        While list.Count < 50
            SyncLock padLock
                For Each i As Integer In list
                    Debug.WriteLine(i.ToString())
                Next
            End SyncLock
        End While
    End Sub
End Class

Can someone explain why the above code does not work? In a similar post someone mentioned that the Interlocked class would be useful but I could not figure out what that is or how that is used. A brief “Interlocked Class For Dummies” explanation would also be appreciated.

  • 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-06-12T09:15:22+00:00Added an answer on June 12, 2026 at 9:15 am

    Ok I think I figured out the problem. I don’t think my thread was deadlocking I think it was just starving my Application.Run() Thread of resources. When I added a Thread.Sleep(500) call after the End SyncLock in the MainLoop method the program worked as expected.

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

Sidebar

Related Questions

can someone help me understand this because it's working in as3 but not so
Can someone help me understand how to make Ryan Bate's screencast on Backbone.js work
I know this regex divides a text into sentences. Can someone help me understand
Please can someone help me understand anonymous access in IIS. I know by when
Can someone help me understand how to write this case statement properly its not
(Specifically, I'm using Backbone Model events) Can someone help me understand how javascript events
Can someone help me on how to setup such a result: Want to know
Can someone help me understand why this gives an output of 0? #include <iostream>
Can someone help me understand this topshelf project...mainly the difference between ConfigureServiceinIsolation and ConfigureService.
Eclipse crashed and won't restart. Can someone help me understand what the problem is?

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.