简体   繁体   English

DLL上WinForm上的事件处理按钮

[英]Event-Handling buttons on WinForm from a DLL

I have written a C# utility class (DLL), which gets called by a windows application. 我编写了一个C#实用程序类(DLL),该类由Windows应用程序调用。 Within the windows application, I have setup a backgrounderWorker to allow for time-consuming code not to hog the windows application. 在Windows应用程序中,我设置了backgrounderWorker以允许耗时的代码不占用Windows应用程序。 The time-consuming code is in the utility class DLL. 耗时的代码位于实用程序类DLL中。 Now, I setup two buttons on the windows application a 'submit' button - where the time-consuming code gets called in the utility class and a 'cancel' button. 现在,我在Windows应用程序上设置了两个按钮:“提交”按钮-在实用程序类中调用了耗时的代码,并在其中单击了“取消”按钮。

I would like the 'cancel' button to stop the backgroundWorker code if clicked. 我希望单击“取消”按钮可以停止backgroundWorker代码。 Problem is the the 'Cancel' button is in the windows application and the code is in the DLL. 问题是Windows应用程序中的“取消”按钮和DLL中的代码。 So is there a way for me to maybe attach the 'cancel' button's 'onClick' eventHandler to the DLL and then have the DLL periodically check to see if the button was pressed? 那么,有没有一种方法可以将“取消”按钮的“ onClick”事件处理程序附加到DLL,然后让DLL定期检查以查看按钮是否被按下?

BTW, the cancel button does work up until the DLL code gets initiated. 顺便说一句,取消按钮的工作直到启动DLL代码。

Am I correct in my thoughts or is there a better way? 我的想法正确还是有更好的方法? Any help would be appreciated. 任何帮助,将不胜感激。

Yes, It is possible to register to events between projects. 是的,可以注册项目之间的事件。 all you have to do is have a reference of the winforms application inside your dll, and have the cancel button public. 您所要做的就是在您的dll中引用Winforms应用程序,并让“取消”按钮公开。 then you can register to any of it's events inside the dll code. 那么您可以在dll代码中注册任何事件。 However , I'm not sure that this is the best way to do what you want. 但是 ,我不确定这是否是执行所需操作的最佳方法。 I think a better way to do it is have a method called Cancel inside the dll and have the cancel button click event call this function. 我认为一种更好的方法是在dll中有一个名为Cancel的方法,并具有cancel按钮click事件调用此函数。 This way your dll is less dependent on your winforms application. 这样,您的dll较少依赖于Winforms应用程序。

I actually went back and looked at some documentation I had about events and event handling. 实际上,我回头查看了一些有关事件和事件处理的文档。 I did the following in the DLL: 我在DLL中做了以下操作:

  1. Made a public class an added an eventHandler, my own custom eventArgs, and a subclass (eventWatcher) to host an event (call it onCancel) 为公共类添加一个eventHandler,我自己的自定义eventArgs和一个子类(eventWatcher)以托管事件(在onCancel上调用)
  2. I registered eventWatcher to listen for the OnCancel event in the DLL. 我注册了eventWatcher来监听DLL中的OnCancel事件。
  3. I made the eventWatcher public so it can be consumed in the client app. 我将eventWatcher公开,因此可以在客户端应用程序中使用它。

In the client app, I did the following: 在客户端应用程序中,我执行了以下操作:

  1. I modified the cancel button to raise the onCancel event. 我修改了“取消”按钮以引发onCancel事件。 This allowed me to assign a method in the DLL to handle the graceful exiting of the time-consuming code in the DLL. 这使我可以在DLL中分配一个方法来处理DLL中耗时代码的正常退出。

It now works as expected! 现在可以正常工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM