Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UWP: "In foreground" context warning + not working #3249

Closed
tipa opened this issue Mar 28, 2024 · 7 comments · Fixed by #3372
Closed

UWP: "In foreground" context warning + not working #3249

tipa opened this issue Mar 28, 2024 · 7 comments · Fixed by #3372
Labels
Bug Something isn't working Good First Issue Good for newcomers UWP

Comments

@tipa
Copy link

tipa commented Mar 28, 2024

Package

Sentry

.NET Flavor

.NET

.NET Version

.NET Standard 2.0

OS

Windows

SDK Version

4.2.1

Self-Hosted Sentry Version

No response

Steps to Reproduce

  • Use Sentry in a UWP app
  • Build in Release mode
  • trigger crash while app window is in foreground

Expected Result

  1. no warning during build
  2. "In Foreground" context shows as "True" in Sentry

Actual Result

  1. Warning during build:
C:\Users\user\.nuget\packages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): warning :
       MCG : warning MCG0007: Unresolved P/Invoke method 'user32.dll!GetWindowThreadProcessId' for method 'System.Int32 Sentry.Internal.ProcessInfo.GetWindowThreadProcessId(System.IntPtr, System.Int32)'. 
Calling this method would throw exception at runtime. 
Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. 
If for some reason your P/Invoke does not satisfy those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP A 
  1. "In Foreground" shows as "false" in Sentry
@tipa
Copy link
Author

tipa commented Mar 28, 2024

Some more context:

The code was introduced in this PR: c0e9a3d

I copied the code and executed it in both a UWP and a WinUI3 / WinAppSDK app:

  • in the WinUI3 app, the code worked as expected, the ApplicationIsActivated method returned true when the window was in the foreground and false, if it was not
  • In the UWP app, the ApplicationIsActivated method always returned false

Perhaps, on UWP the Window.Current.Visible property could serve as a workaround, however it returns true even when the window is visible but not focused. Another option is to use CoreWindow.GetForCurrentThread().ActivationMode which returns ActivatedInForeground when the app is in foreground and focused

@jamescrosswell
Copy link
Collaborator

Thanks so much for the sleuthing @tipa ! CoreWindow.GetForCurrentThread().ActivationMode sounds promising...

@jamescrosswell
Copy link
Collaborator

We can't use CoreWindow.GetForCurrentThread() without adding an unacceptable dependency to the core Sentry package or, alternatively, creating a separate Sentry.UWP package... which is unlikely any time soon.

It looks like the original issue with GetForegroundWindow is part of the basic design for UWP (UWP apps don't have any Window Handle).

At this point, I can't see an easy way around this one... @bitsandfoxes @vaind any ideas?

@tipa
Copy link
Author

tipa commented Apr 8, 2024

If there's no easy solution to the problem, setting the Foreground context could just be disabled on UWP so that at least the build warning disappears? If a developer wants to add the information, he could still enrich the event himself

@bitsandfoxes
Copy link
Contributor

at least the build warning disappears

I think this is the way to go.

@tipa
Copy link
Author

tipa commented Apr 17, 2024

Just to confirm that the GetWindowThreadProcessId method is not available on UWP:
image

@bitsandfoxes
Copy link
Contributor

We discussed maybe just suppressing the waring via pragma as this is getting caught by the ProcessInfo already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Good First Issue Good for newcomers UWP
Projects
Status: Done
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants