Event-Handling buttons on WinForm from a DLL - c#

I have written a C# utility class (DLL), which gets called by a windows application. Within the windows application, I have setup a backgrounderWorker to allow for time-consuming code not to hog the windows application. The time-consuming code is in the utility class 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.
I would like the 'cancel' button to stop the backgroundWorker code if clicked. Problem is the the 'Cancel' button is in the windows application and the code is in the 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?
BTW, the cancel button does work up until the DLL code gets initiated.
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. then you can register to any of it's events inside the dll code. 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. This way your dll is less dependent on your winforms application.

I actually went back and looked at some documentation I had about events and event handling. I did the following in the DLL:
Made a public class an added an eventHandler, my own custom eventArgs, and a subclass (eventWatcher) to host an event (call it onCancel)
I registered eventWatcher to listen for the OnCancel event in the DLL.
I made the eventWatcher public so it can be consumed in the client app.
In the client app, I did the following:
I modified the cancel button to raise the onCancel event. This allowed me to assign a method in the DLL to handle the graceful exiting of the time-consuming code in the DLL.
It now works as expected!

Related

Hook button press event in another application

I’ve to develop an application that has to track the button press event of a button which in another application. After some research I found that Windows Hooks might work for me and I’d develop a sample application that hooks the button press event in another application.
The problem I’m facing with that sample application, gets the control after the button is pressed whereas I need to get the control before the button is pressed i.e., First the sample application should get the control so, that it can grab the information from that dialog and, then after the control goes to the actual event handler of that button in other application.
I’ve already tried WH_CALLWNDPROC, WH_CALLWNDPROCRET, WH_GETMESSAGE, & WH_SYSMSGFILTER. Using WH_CALLWNDPROC hook I’m able to hook the button press but my application gets the control after the button press event handler is executed but my requirement is to get the control before the button press event handler is executed.
I’ve used the Wilson GlobalHooks to hook the application in C#. Here is the link to it.

c# detect which messagebox (originated in another app) button has been pressed

Another application displays a messagebox (with a unique text inside it), user chooses Yes/No.
How to detect what he pressed in c#? (best in .Net up to 3.5). I could do polling with FindWindowEx (on another thread) but how to detect what button had been pressed? Also I don't think polling is the best way to do the job.
I need to know what the user has chosen in another app, so I can react accordingly in my own app. I don't have access to the other app's source code. Also to make it clear I don't want to click any of the buttons myself. I'm not afraid of a bit of c++, winapi and pinvoke
To monitor UI events in another application you can use UI Automation. To solve your specific problem you need to subscribe to a particular event (see Subscribing to UI Automation Events). To do so call IUIAutomation::AddAutomationEventHandler with a UIA_Invoke_InvokedEventId Event Identifier.
While UI Automation can be used to solve your problem, it is an assistive technology, mainly to enable accessibility needs and automated UI testing.
You could use either Anonymous or Named Pipes or WCF(Windows Communications Foundation).

The Button in WinForms has a CLICK event. But who tells the Button object that it has been clicked?

I am learning C#. I was going through the Events and Delegates part of the language. I am working on a WinForms application for educating myself. I tried looking deep for understanding Buttons and how they work. I found the following:
1) There is a line public partial class Form1 : Form in my default Form1.cs file. This is a partial class.
2) I also have a Form1.Designer.cs class file that has a line partial
class Form1. Now the files mentioned in 1) and 2) combine to form a
full class.
3) The From1.Designer.cs file has a lot of statements that eventually
create the button object. It also has a statement that is of
particular interest to me:
this.btn_BaseBuildLocation.Click += new System.EventHandler(this.btn_BaseBuildLocation_Click);
This statement adds a custom function to the delegate Click. This
delegate is declared in the Control class (System.Windows.Forms.dll) as follows:
public event EventHandler Click;
4) The EventHandler is a delegate defined in System.EventHandler.cs
(mscorlib.dll).
5) The Button class inherits Control class and thus has access to the
Click EventHandler.
6) The Button class has all the logic to handle the flow once it knows
that someone has clicked it. I had a look at the Button class used in
Mono for understanding the inner details. I do this for almost all
classes that I want to learn.
7) All this is extremely beautiful. But I was troubled by the fact
that I did not know how the Button object knows that it has been
clicked.
8) I went through VC++ and how it handles the events. I found a lot
about Message Loops, Event Queues etc...
Questions:
1) Is the VC++ way of handling events the same as .NET's?
2) If so, is there a way to look into those details?
Any help would be appreciated.
Thanks.
A Button is, technically, a Window. It has a Window handle.
That means that the Dispatcher will route keyboard and mouse events to the Button when appropriate. The Button has internal logic to determine when a MouseDown and a MouseUp event constitute a valid click and then it raises the Click event.
1) Is the VC++ way of handling events the same as .NET's?
Yes obviously, handling is same, it is done by capturing the window messages and respond accordingly. .Net provides a wrapper around window handles 'NativeWindow class' which is low level encapsulation of a Window Handle, System.Windows.Forms.Control is the base class for all Controls which internally uses decendant of NativeWindow named ControlNativeWindow which passes all Messages to Control.
If so, is there a way to look into those details?
Yes, dig into Control class Through Reflector
hope this helps
A WinForms Button is a managed wrapper around the unmanaged Windows type which is created and managed via a set of Win32 API calls that .NET performs P/Invoke on.
Deep down, the button subscribes to the same Window Event Loop (or Message Pump if you prefer) which drives the Win32 API calls you may have seen in VC++ examples. The unmanaged Windows runtime puts events (like "this button has been clicked") onto the event queue. When the loop executes, the queued event is picked up by the relevant control and is propagated into a "managed" event which is when you are able to observe it.
In essence, the Windows runtime is providing much of the infrastructure and .NET only provides a convenient set of wrappers which make it easy to work with the clunky old Win32 libraries.
You can discover a lot of this for yourself if you use Reflector and dig into Button and Control to just see where the .NET code ends and the unmanaged Win32 calls begin.
In WinForm appplicatio you have Program class with Main method.
There is always one line there:
Application.Run(new YourMainForm());
It begins running a standard application message loop on the current thread.
That's the "starting point" for events.
Answer to this question explains it prertty well and also links to some sources

Getting events of another application

In my c# application I need to detect when a user clicks one of two buttons in a different, third party application.
I am able to get the Handle of the application but the MainWindowHandle returns 0.
I tried WndProc but for some reason the event will not fire in my application.
How can I get/intercept the button click event from that application into mine?
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult to implement in C#. This project on Codeplex attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue. Put simply, this lets you implement any type of global Windows hook from managed code: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

How to clear folder contents on application close

i want to delete all items from my custom made folder, but i want to do it when the application closes as the user no longer needs a handle on these files anymore.
Where in the c# code should i write such a method? For example is there a application.shutdwon event or something :$
Thanks
Winforms has an ApplicationExit event.
WPF has an Exit event.
You can write your cleanup code in the event handler.

Categories