简体   繁体   中英

C# code works in IDE but not in generated exe

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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