xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.47k stars 513 forks source link

ScreenCaptureKit hangs on awaiting GetShareableContentAsync #17350

Closed icanos closed 1 year ago

icanos commented 1 year ago

Description

When setting up a minimal .NET Console application and changing TargetFramework to net6.0-macos and trying to get a list of windows or displays, the application hangs on awaiting the result of GetShareableContentAsync method.

The "same" code works in a minimal Swift xcode project.

Here is the code I'm running, when running using the debugger in Rider, I get "Hello world!" and then nothing more. https://github.com/icanos/screencapturekit-issue

Simply clone and run dotnet restore followed by starting it in Visual Studio or Rider. When running the same dotnet code in the Terminal, no output is given at all, neither "Hello world!" nor a list of applications, nothing. The application also hangs in the Activity Monitor and needs to be quit from there (I guess that is because of the awaiting the results of GetShareableContentAsync).

When running this code, I get "Hello, World!" followed by a complete list of windows: https://github.com/icanos/screencapturekit-swift

Configuration

Other information

Output of my dotnet --info

.NET SDK:
 Version:   7.0.102
 Commit:    4bbdd14480

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.1
 OS Platform: Darwin
 RID:         osx.13-arm64
 Base Path:   /usr/local/share/dotnet/sdk/7.0.102/

Host:
  Version:      7.0.2
  Architecture: arm64
  Commit:       d037e070eb

.NET SDKs installed:
  6.0.101 [/usr/local/share/dotnet/sdk]
  6.0.301 [/usr/local/share/dotnet/sdk]
  6.0.402 [/usr/local/share/dotnet/sdk]
  6.0.403 [/usr/local/share/dotnet/sdk]
  6.0.405 [/usr/local/share/dotnet/sdk]
  7.0.100 [/usr/local/share/dotnet/sdk]
  7.0.102 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.10 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.10 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  x64   [/usr/local/share/dotnet/x64]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Environment

Version information ``` Visual Studio Community 2022 for Mac Version 17.4.3 (build 21) Installation UUID: c2670223-7707-4343-9bff-1c0ea56d464f Runtime .NET 6.0.12 (64-bit) Architecture: Arm64 Roslyn (Language Service) 4.4.0-6.22578.12+3c6ab8e1715e5b080fb7bb77070810ab71e09387 NuGet Version: 6.3.1.1 .NET SDK (Arm64) SDK: /usr/local/share/dotnet/sdk/7.0.102/Sdks SDK Versions: 7.0.102 7.0.100 6.0.405 6.0.403 6.0.402 6.0.301 6.0.101 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks .NET SDK (x64) SDK Versions: 3.1.426 3.1.425 3.1.424 .NET Runtime (Arm64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.2 7.0.0 6.0.13 6.0.11 6.0.10 6.0.6 6.0.1 .NET Runtime (x64) Runtime: /usr/local/share/dotnet/x64/dotnet Runtime Versions: 3.1.32 3.1.31 3.1.30 Xamarin.Profiler Version: 1.8.0.19 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler Updater Version: 11 Apple Developer Tools Xcode 14.2 (21534) Build 14C18 Xamarin.Android Version: 13.1.0.1 (Visual Studio Community) Commit: xamarin-android/d17-4/13ba222 Android SDK: /Users/westin/Library/Android/sdk Supported Android versions: 12.1 (API level 32) 12.0 (API level 31) 13.0 (API level 33) SDK Command-line Tools Version: 7.0 SDK Platform Tools Version: 33.0.2 SDK Build Tools Version: 32.0.0 Build Information: Mono: a96bde9 Java.Interop: xamarin/java.interop/d17-4@fcc33ce2 SQLite: xamarin/sqlite/3.39.3@23e1ae7 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.16.1 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Android SDK Manager Version: 17.4.0.54 Hash: 6eabb9e Branch: remotes/origin/d17-4 Build date: 2023-01-12 17:32:35 UTC Android Device Manager Version: 0.0.0.1206 Hash: 886af39 Branch: 886af39 Build date: 2023-01-12 17:32:35 UTC Xamarin.Mac Version: 9.0.0.27 (Visual Studio Community) Hash: 933c6c2c9 Branch: xcode14.1 Build date: 2022-11-22 02:00:36-0500 Xamarin.iOS Version: 16.1.1.27 (Visual Studio Community) Hash: 933c6c2c9 Branch: xcode14.1 Build date: 2022-11-22 02:00:37-0500 Xamarin Designer Version: 17.4.0.136 Hash: d49c9ff6d3 Branch: remotes/origin/d17-4 Build date: 2023-01-12 17:32:30 UTC Build Information Release ID: 1704030021 Git revision: 26eac6764d85f12fe50ee0d45a7cd0b266be7b23 Build date: 2023-01-12 17:30:14+00 Build branch: release-17.4 Build lane: release-17.4 Operating System Mac OS X 13.1.0 Darwin 22.2.0 Darwin Kernel Version 22.2.0 Fri Nov 11 02:03:51 PST 2022 root:xnu-8792.61.2~4/RELEASE_ARM64_T6000 arm64 ```
rolfbjarne commented 1 year ago

I believe this is because there's no main loop running in your sample project.

Creating a complete macOS app like this works just fine:

  1. dotnet new macos
  2. Apply patch: https://github.com/rolfbjarne/screencapturekit-macos/commit/1b999b87b5ae23129883ea2aa159ae500a57a2ca
  3. Run:
$ ./bin/Debug/net7.0-macos/osx-x64/screencapture-macos.app/Contents/MacOS/screencapture-macos
Window: Microsoft Teams Notification
Window: Default
Window:
Window:
[...]

Complete repo: https://github.com/rolfbjarne/screencapturekit-macos

icanos commented 1 year ago

Thanks for looking into it.

If I'd like to use the functionality in say an Avalonia application that is cross platform, how would I then do that? I tried the following as well without success:

NSApplication.Init();

Console.WriteLine("Hello");

NSRunLoop.Main.InvokeOnMainThread(async () =>
{
   var test = await SCShareableContent.GetShareableContentAsync(false, true);
   foreach (var w in test.Windows)
      Console.WriteLine("Window: " + w.Title);
});

NSRunLoop.Current.RunUntil(NSDate.DistantFuture);

I'm basically trying to create a xplat library that can capture screens of windows/desktop and then run in an Avalonia application.

rolfbjarne commented 1 year ago

This works for me: https://gist.github.com/rolfbjarne/f824a3b8297aa055ae6cf3600c76aa67

rolfbjarne commented 1 year ago

How are you running your app?