I have a tray app with an event handler. Whenever the event is triggered I want to inform the user via a corresponding popup image about a status. This image should appear for around 500 ms in the center of the screen for which I need a form with a picturebox.
I tried to display the form via new & close and via show & hide but both are not working as expected. Either the form is showing and hiding itself only once at the start (when I create and show it via the constructor of the context class) but not for further event triggers (I can see the forms boundaries but its grey and hanging) or the form is not showing at all (when I only create and show it via delegate from the event handler to a method of the context class) or it is only seen for half a millisecond or I get a thread error (even when I do not use any additional threads) when the second trigger happens.
I am really lost here and do not know which would be the correct approach.
UPDATE & SOLUTION:
As I use a tray app (starting with an ApplicationContext instead of a Form) the UI thread has to be handled manually, see How to invoke UI thread in Winform application without a form or control.
As explained in the top answer, you need to add a method for the Application.Idle event where you put the instanciation of your controller/handler in order to prevent duplicate instanciations and thereby a deadlock of your UI thread.
For the instanciation and within the controller/handler class you need to add a UI invoke reference of the type Action<Action> which can be used for any UI manipulations as those are directly executed in the UI thread.
Final code fitting to the explanation in the OP under UPDATE & SOLUTION:
Program.cs is the main entry point of our tray app:
MyContext.cs prevents duplicate instanciation via
Application.Idleevent method and has anUpdate()method where it uses a handler value to update some UI elements:MyHandler.cs needs a UI invoke reference by which it can directly call into the UI thread and thereby manipulate UI elements:
MyForm.cs uses a timer by which it can close itself:
This works even when the handler event (system trigger)
Something_OnSomethingis called faster again than the form timer eventTimer_Tickcan close the form.