I am working on an application which requires a form to be activated on receiving some external events. For this as a POC I have created the following sample code.
FormRecvr:
1) Create a simple form with textbox control
2) Have a TCP listner listning on some port to receive the event
3) On the reception of event, activate the form i.e bring it to front so that it has focus and user can type some text.
FormSender:
1) Create a simple form with two button controls, one to activate the rcvr form by sending an event through socket and the other to exit the application.
2) Create a TCP socket with the listner and send the event.
After executing this simple program, what I have noticed is the receiver form doesn't get the focus. The taskbar menu of that app keeps blinking. The window doesn't come as foreground.
I have tried the following to bring it to focus.
this.TopMost = true;
this.Activate();
this.TopMost = false;
I have even tried SetForegroundWindow Win32 API. Even with that it doesn't work. It works itermittently. 8 out of 10 times, it gives the above mentioned issue. The form will be visible but focus will not be there, the task bar menu will be falshing. We have to click on the form or on the flashing task menu bar to get the focus.
Please let me know how to resolve this issue.