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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:59:46+00:00 2026-06-11T15:59:46+00:00

This seems like a basic question, but Google has not helped me… How do

  • 0

This seems like a basic question, but Google has not helped me… How do I do basic thread locking in C#.NET WPF? In my fairly simple user application, I have multiple threads performing time-intensive operations that save their results into one common data structure or read from this common data structure. I’d like to be able to lock() and unlock() around access to this data structure to maintain thread safety. However, there are many different places in my code that I access this data structure from, not just one. What .NET objects are used to implement this locking?

  • 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-11T15:59:48+00:00Added an answer on June 11, 2026 at 3:59 pm

    try this:

    public static class LockHolder
    {
        public static object Lock = new object();
    }
    

    then, in your code, use:

    lock(LockHolder.Lock)
    {
        //your safe code
    }
    

    http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.71).aspx

    I guess a little more info may be helpful. IIRC, the lock(object o) is basicly a macro that will be expanded by the compiler and then restrict access to your safe code, which is what you’d expect. What isn’t necessarily obvious is that any object o can be a lock, and each o is needed for each safe lock. if I have

    object a = new object();
    object b = new object();
    

    and perform a lock(a) then that will not prevent access to something with a lock(b) around it. Most code samples show a lock(this) which will lock to the current instance of the particular class, which is not what you want if you need a global lock and have multiple instances of that class running around.

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

Sidebar

Related Questions

This seems like a basic question, but I haven't found any clear answers. Essentially,
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
This seems like a repeated question but i'm not able to get my answer.
This seems like a very basic question but I couldn't find any help on
All, This would seem like a fairly basic asp.net question - but in all
this seems like a basic question but I can't figure out the best implementation.
This seems like a really basic question that has been surprisingly hard to find
This seems like quite a silly basic question but it seems something I have
Seems like a basic question but I can't find this anywhere. Basically I've got
This seems like a basic question but i have doubts maybe because i am

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.