1

I am trying to open an always on top form from a system tray application, basically for showing some instant notifications.

For achieving always on top behavior, I followed the instructions in the reference1 and reference2.

The code in the references works only if I add following code on Frm_Load.

 private void Frm_Load(object sender, EventArgs e) {
    this.TopMost = true;
    this.Activate();
    this.Focus();
    this.BringToFront();
    SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);  // code from references
}

The code only works when I run it in Visual Studio 2019. It does not work at the built exe. In the exe form does not open as top, also not stay always on top, it stays under the other windows. I disabled the visual studio code optimization in the build settings but it did not help.

What can cause that the code working differently in the IDE and in the exe? How can I solve this issue, and show an on top window like Winamp?

zacoder
  • 91
  • 11
  • 3
    https://devblogs.microsoft.com/oldnewthing/20050607-00/?p=35413 – Hans Passant Nov 13 '22 at 23:10
  • That is not the case in here, I mean a newer on top program can open a form over mine, that is not my problem, there is not such "What if two programs did this?" issue in here. The problematic thing in here is the code working differently at the outside of the IDE. – zacoder Nov 14 '22 at 12:50
  • This is also the case with exe generated by replicating the code and I recommend you report this issue in DC. https://developercommunity.visualstudio.com/report?space=8&entry=problem – Lei Zhang-MSFT Nov 17 '22 at 02:23

1 Answers1

1

The problem is originating from my inappropriate usage of opening forms in a thread. I noticed that I initiated to open the form from a listener thread(listens a notification from websocket). Somehow visual studio manages this and opens the form as always-on-top per my expectation.

When I change the opening code of the form from thread to the main form (with method invoker), then issue is solved and the exe also acts the same as debug. The code inside the listener thread like folowing:

GuiManager.getInstance().getMainForm().Invoke((MethodInvoker)delegate () {
                        PopupUtil.showPopup();
                    });

I hope it would be helpful who stuck at a similar issue with debug env. and released exe.

zacoder
  • 91
  • 11