线程同步和解耦类
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用带有等待事件句柄的信号解决了这个问题,我只想提一下 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.
您可以使用 ManualResetEvent 来实现此目的
You can use ManualResetEvent for this