C#-线程和将父级传递给子级

发布于 2024-11-14 18:07:50 字数 646 浏览 2 评论 0原文

我只是想知道将父级传递给子级所涉及的问题,是否应该这样做等,以便子级可以访问父级的功能,在这种情况下它涉及线程。我的情况如下:

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(); }
}

基于上述,我想知道是否会发生任何约定冲突,或者当您执行此类操作时会出现问题,这样做是否不好?我知道这肯定会带来一些线程安全问题。但我的总体问题是这样做是可以的,它会带来一些其他问题吗?我将如何更新主线程中的值? 我想要它分开的原因是因为 B 类中有一个计时器(不在所示的代码中),当它用完时会执行某些操作,然后告诉 A 类它已完成,重置计时器并返回睡眠状态,直到计时器到达又用完了,否则我该怎么办?

〜问候,

海因里希

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-21 18:07:50

查看代码,您似乎没有任何线程问题。如果您正在处理相同的变量,您可能会引入竞争条件,但这对于您提出的情况来说并不特殊。

您可以像任何其他多线程情况一样对待这种情况,并锁定可能被多个线程访问的资源。

我会推荐以下在线书籍: http://www.albahari.com/threading/

我不知道您并不认为您真的必须像现在这样紧密地连接它们,您要做的只是在线程之间传递消息或状态。因此,我建议不要将它们连接得如此紧密,是为了减少耦合。

我引用的网站包含许多不同的信号技术。根据您的需要选择最简单的。我需要更多关于您的具体要求的详细信息,以便为您选择一个。

Looking at the code, you don't appear to have any threading issues. You might introduce a race condition if you are working on the same variables, but that isn't special to the situation you propose.

You would treat this like any other multi-threaded situation and lock resources that might be accessed by multiple threads.

I would recommend the following online book: http://www.albahari.com/threading/

I don't think you really have to connect them as tightly as you are, what you are trying to do is simply pass messages or states between threads. So the reason I would recommend not having them so tightly connected is to reduce coupling.

The website I referenced contains many different signaling techniques. Pick the simplest for your needs. I would need more details about your exact requirements to pick one for you.

优雅的叶子 2024-11-21 18:07:50

处理您正在做的事情的另一种方法是由 B 引发事件并由 A 处理该事件。这样你就不必将 A 传递给 B。我不知道你的真实结构是什么,但可以说 B 的线程函数做了一些更复杂的事情,而 A 实现了 IDisposable。如果 A 在 B 调用 A 上的方法之前被释放,会发生什么。对我来说,处理这种情况的更简洁的方法是让 B 引发一个事件,然后 A 注册它。

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);
         }
    }    
 }

注意 - 您应该在 B 类中实现 IDisposable 并处理 ManualResetEvent,我只是懒得为示例代码执行所有这些操作,只是想让您了解如何使用事件来指示工作情况完毕。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文