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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:27:34+00:00 2026-06-11T20:27:34+00:00

Example code: MyObject myObject = new MyObject (); public void FunA () // accessed

  • 0

Example code:

MyObject myObject = new MyObject ();

public void FunA () // accessed from thread 1 (when user click a button)
{
    myObject = null;
    // do some stuff
    myObject = new MyObject ( someNewValues );
}

public void FunB () // accessed from thread 2 (calling using timer or smth.)
{
    int x = myObject.ReadX ();
}

In most cases it works OK, but when FunA will make myObject null and in same time FunB will access it, application will crash.

Question: How to do that, when thread 1 will access FunA, thread 2 cannot enter FunB?

Update: I tried locks before, but the problem is FunB is calling event to thread 1 (GUI) (guiForm.Invoke) and thread 1 will stuck before FunA { lock… }.

  • 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-11T20:27:35+00:00Added an answer on June 11, 2026 at 8:27 pm

    You can lock myObject so that only the current thread can use it.

    public void FunA () // accessed from thread 1 (when user click a button)
    {
      lock(myObject)
        {
           myObject = null;
           // do some stuff
           myObject = new MyObject ( someNewValues );
        }
    }
    

    This will prevent FunB from accessing myObject until it has been released by the lock.

    Update

    As pointed out in the comments, it’s probably not a good idea to modify the object that you have a lock on. In this case you can simply have an object that you lock and an object that you modify such that:

    var lockObj = new object();
    var myObject = new MyObject();
    
    
    public void FunA () // accessed from thread 1 (when user click a button)
    {
      lock(lockObj)
        {
           myObject = null;
           // do some stuff
           myObject = new MyObject ( someNewValues );
        }
    }
    
    public void FunB () // accessed from thread 2 (calling using timer or smth.)
    {
        lock(lockObj)
        {
           int x = myObject.ReadX ();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
Just some example code here, but I have lists of strings that I want
Take the following code example: var myObject = {}; var i = 100; while
My code looks like this: myObject.myMethod('imageCheck', function () { var image = new Image();
Given the following method: public async Task<MyObject> DoSomethingAsync() { // do some work await
Writing GWT application. I have CellTable and a code that i'v got from google-example
I have simplified example: XAML: <CheckBox IsChecked={Binding Path=IsSelected, Mode=TwoWay} Name=cb /> <Button Name=button1 Click=button1_Click
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1

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.