0

My WPF app get a error count from restapi every 15s, when the error count is more than 0, my app should show in front and cant be covered by other app to notice the user to handle the error.

The main code is below.

if (Application.Current.MainWindow.WindowState != WindowState.Normal) { 
    Application.Current.MainWindow.WindowState = WindowState.Normal;
}
Application.Current.MainWindow.Topmost = true; 

Every time I changed my code and rebuild it, It will work for the first time. But when I closed my app and run it again, It doesn't work.The app doesn't go to topmost.

I tried setting topmost to false before setting it to true.But didn't work.

I'm confused why the same code behaviors different in first time and second time. Did I forget to handle something when I close the app?(I do nothing now.) Can anyone tell me why and give me a solution. Thank you!

Edit:

When I generate a installer with the code and install it, the app installed works every time. The problem only occurs when I debug with Visual Studio.

ender1986
  • 95
  • 1
  • 9
  • Perhaps you should check this thread: https://stackoverflow.com/a/383708/8306962, It may have some answers that help you. Have you tried using `mywindow.Activate()`? – Issung Dec 03 '19 at 02:01
  • @JG3 Thank you! Your comment is helpful. It works. But even without adding this code, it worked by running the installed app instead of debugging in IDE. So I still don't know what makes it happen. It seems like my test failed but my app is OK~~ – ender1986 Dec 03 '19 at 06:47

0 Answers0