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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:27+00:00 2026-05-23T01:42:27+00:00

I am just wondering about problems involved passing a parent to child, should it

  • 0

I am just wondering about problems involved passing a parent to child, should it be done etc, so that a child can access functionality from the parent, in this case it involves threads. My scenario follows:

public class A
{
    public A() 
    { 
        B b = new B(this);
        Thread thread = new Thread(new ThreadStart(b.GO));
    }
    public string DoSomething() { return "Something Done"; }
}

public class B
{
    A _a;
    public B(A a) 
    {
        _a = a;
    }
    public void GO() { _a.DoSomething(); }
}

Based on above I’m wondering about any convention clashes the occur, or problems that come into effect when you do something like this, is it bad to do this? I know that there definitely brings up some thread safety issues. But my overall question is ok to do this, does it bring up some other issues? How would I update values in the main thread?
The reason why I want it separate is because class B has a timer in it(not in shown code) that when it runs out does somethings, then tells class A that it has finished, resets the timer and goes back to sleep until the timer runs out again, how would I do that otherwise?

~Regards,

Heinrich

  • 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-23T01:42:27+00:00Added an answer on May 23, 2026 at 1:42 am

    Another way to handle what you are doing is for B to raise an event and for A to handle the event. That way you don’t have to pass A into B. I don’t know what your real structure is, but lets say that B’s thread function does something more complicated and A implements IDisposable. What happens if A is disposed before B gets to the point that it is calling a method on A. To me the cleaner way to handle that situation is to have B raise an event and A register for it.

    public class A
    {
        B _b;
    
        public A()
        {
            _b = new B();
            _b.DidSomething += HandleDidSomething;
        }
    
        private void HandleDidSomething(object source, EventArgs e)
        {
            // Handle the B did something case
        }
    
        public void WaitForBToFinish() { _b.DoneDoingThings.WaitOne(); }
    }
    
    public class B
    {
        Event EventHandler DidSomething;
    
        ManualResetEvent DoneDoingThings = new ManualResetEvent(false);
    
        public B() {}
    
        public void StartDoingThings()
        {
            new Thread(DoThings).Start();
        }
    
        private void DoThings()
        {
             for (int i=0; i < 10; i++)
             {
                 Thread.Sleep(1000);
                 OnDidSomething(new EventArgs());
             }
    
             DoneDoingThings.Set();
        }
    
        private void OnDidSomething(EventArgs e)
        {
             if (DidSomething != null)
             {
                  DidSomething(e);
             }
        }    
     }
    

    Note – You should implement IDisposable in class B and dispose of the ManualResetEvent, I just am too lazy to do all that for sample code, and just wanted to give you an idea about using events to signal work was done.

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

Sidebar

Related Questions

I was just wondering about something. I have a frame that loads pages and
Can this object pool cause visibility problems with multiple threads? I'm specifically wondering about
Just wondering about the performance impact of copying very large php variables. For example
Just wondering about this code below... when I turn off my internet connection and
Just wondering about some practice about this; I have made a simple visual C#
I'm just wondering about large projects - say an airlines reservation system, how many
I'm new to web development and am just wondering about best practices for java
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
I was just wondering on how i go about displaying the balance for the

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.