C#“持久”调用DLL
我正在从自制的 DLL 中调用一个方法。此方法触发一个事件,并由类内的 EventHander 引发。
问题在于,执行的控制权返回到主程序(图中的 WebClass),主程序在事件处理程序能够捕获事件之前调用该类。
这是它应该如何工作的小图 http://dl.dropbox.com/u/5654637/model1.png
这就是它的实际作用 http://dl.dropbox.com/u/5654637/model2.png
有没有办法调用类的方法并确保它将在将控制权返回给调用者之前执行所有任务?
谢谢
I am calling a method from a self made DLL. This method triggers an event and it is raised by an EventHander inside the class.
The problem is that the control of the execution returns to the main program (WebClass in the diagram) which makes the call to the class before the eventhandler is able to catch the event.
Here is a little diagram of how it should work
http://dl.dropbox.com/u/5654637/model1.png
And this is what it actually does
http://dl.dropbox.com/u/5654637/model2.png
Is there a way to make the call to the class' method and make sure it will execute all tasks before returning control to the caller?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我必须看看你的另一个 DLL 中发生了什么。默认情况下,它应该按您想要的方式工作。除非您采取某些措施使其异步,否则 .NET 中的事件都是同步的。当您触发事件时,触发该事件的代码将不会继续执行,直到所有侦听该事件的委托都已执行完成(或者其中一个委托抛出异常)。
说代码“等待”直到委托完成执行甚至不是一个准确的说法,事件可以被认为是委托的集合,当事件被触发时,它会循环所有委托并执行它们一次一个。
I'd have to see what it happening inside your other DLL. By default, it should work as you want. Unless you do something to make it asynchronous, events in .NET are synchronous. When you fire an event, the code that fires that event will not continue to execute until all delegates that are listening to the event have been executing to completion (or one of the delegates throws an exception).
Saying that the code "waits" until the delegates are finished executing isn't even an accurate statement, an event can be thought of as a collection of delegates, and when an event is fired, it loops through all of the delegates and executes them one at a time.
您可以使用 ManualResetEvent。
在 Method1 中,您等待事件被设置。在事件处理程序 E_DataEvent 中,您正在设置信号。
You could use a ManualResetEvent.
In your Method1 you wait for the event to be set. And in your event handler E_DataEvent you are setting the signal.