线程同步和解耦类

发布于 2025-01-03 19:50:02 字数 1200 浏览 2 评论 0原文

我有 2 个解耦的类,A 类和 B 类,这两个类使用 Mircosoft CAB 框架通过事件发布和事件订阅相互通信:

public class ClassA
    {
        [EventPublication("ActionFired", PublicationScope.WorkItem)]
        public event EventHandler<EventArgs> ActionFired;

        public void MethodA()
        {
            // Some code 1
            DoAction1();
            // Some code 2
        }

        private void DoAction1()
        {
            if (ActionFired != null)
                ActionFired(this, EventArgs.Empty);
        }
    }

    public class ClassB
    {
        [EventSubscription("ActionFired")]
        public void OnActionFired(object sender, EventArgs e)
        {
            // Here some background work using BackgroundWorker
        }

        private void bagroudThread_DoWork(object sender, DoWorkEventArgs e)
        {
            // work 
        }

        private void bagroudThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         {

         }
    }

在 MethodA 中我们调用 DoAction1();触发事件发布,订阅者在后台工作线程中执行其处理程序,这里的问题是我希望“某些代码 2”(在调用 DoAction1() 之后出现)仅在后台工作线程完成后执行,但这里 DoAction1 () 异步执行,'some code 2' 在 classB 后台工作者完成工作之前执行。 如何控制MethodA的执行?

问候

I have 2 decoupled classes, class A and class B, these two classes communicate with each other with event publication and event subscription using Mircosoft CAB framework :

public class ClassA
    {
        [EventPublication("ActionFired", PublicationScope.WorkItem)]
        public event EventHandler<EventArgs> ActionFired;

        public void MethodA()
        {
            // Some code 1
            DoAction1();
            // Some code 2
        }

        private void DoAction1()
        {
            if (ActionFired != null)
                ActionFired(this, EventArgs.Empty);
        }
    }

    public class ClassB
    {
        [EventSubscription("ActionFired")]
        public void OnActionFired(object sender, EventArgs e)
        {
            // Here some background work using BackgroundWorker
        }

        private void bagroudThread_DoWork(object sender, DoWorkEventArgs e)
        {
            // work 
        }

        private void bagroudThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         {

         }
    }

Here in MethodA we call DoAction1(); that fire the event publication, the subscriber execute its handler in a background worker thread, the problem here that I want that 'some code 2' (that comes after calling DoAction1()) execute only after the background worker is completed, but here DoAction1() is executed asynchronously and 'some code 2' is executed before the classB background worker complete the work.
How can I control the execution of MethodA?

Regards

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

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

发布评论

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

评论(2

猥琐帝 2025-01-10 19:50:03

使用带有等待事件句柄的信号解决了这个问题,我只想提一下 Microsoft .Net Framework 4.0 中引入的 ManualResetEventSlim,它是 ManualResetEvent 的另一个版本,它针对短等待时间进行了优化,并且能够选择旋转一组迭代次数。它还具有更高效的托管实现,并允许通过 CancellationToken 取消等待。有关详细信息,请参阅使用事件等待句柄发送信号

Using the signaling with the wait event handles solve the problem, I want just to mention the introduction of ManualResetEventSlim in the Microsoft .Net Framework 4.0 that is another version of ManualResetEvent and it is optimized for short waiting times with the ability to opt into spinning for a set number of iterations. It also has a more efficient managed implementation and allows a Wait to be canceled via a CancellationToken. See Signaling with Event Wait Handles for more information.

野味少女 2025-01-10 19:50:02

您可以使用 ManualResetEvent 来实现此目的

public void MethodA()
{
    // Some code 1
    DoAction1();

    //call wait metod: eventWaitHandle.WaitOne

    // Some code 2
}


private void bagroudThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //call set method:  eventWaitHandle.Set
}

You can use ManualResetEvent for this

public void MethodA()
{
    // Some code 1
    DoAction1();

    //call wait metod: eventWaitHandle.WaitOne

    // Some code 2
}


private void bagroudThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //call set method:  eventWaitHandle.Set
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文