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

Is there a reason why the DISABLE_XAML_GENERATED_MAIN does not work for x86 platforms? #7068

Closed
andrewittorio opened this issue May 6, 2022 · 5 comments
Labels

Comments

@andrewittorio
Copy link

Hello,

I have a WinUI3 Desktop application and I am working to make it single-instanced.

I am following this article: https://blogs.windows.com/windowsdeveloper/2022/01/28/making-the-app-single-instanced-part-3/.

The mechanism works fine, but I Noticed that if I change the solution platform from x64 to x86, the static Main method in the Program class is not called and the single instance is not possible. Is there a reason for that? Where am I doing wrong?

Here the vs project :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>MyApp</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|arm64'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|arm64'">
    <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
  </PropertyGroup>
  

  <ItemGroup>
    <Content Include="Assets\SplashScreen.scale-200.png" />
    <Content Include="Assets\LockScreenLogo.scale-200.png" />
    <Content Include="Assets\Square150x150Logo.scale-200.png" />
    <Content Include="Assets\Square44x44Logo.scale-200.png" />
    <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
    <Content Include="Assets\StoreLogo.png" />
    <Content Include="Assets\Wide310x150Logo.scale-200.png" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
    <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.197" />
    <PackageReference Include="WinUIEx" Version="1.4.2" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
       Tools extension to be activated for this project even if the Windows App SDK Nuget
       package has not yet been restored -->
  <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnablePreviewMsixTooling)'=='true'">
    <ProjectCapability Include="Msix" />
  </ItemGroup>

</Project>

Thank you. Best regards,

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label May 6, 2022
@MikeHillberg
Copy link
Contributor

When you say it's not called, do you mean that the app doesn't start? Or that it's calling the generated one instead?

Note that if you create your own Main by copying the generated one and then setting that constant, in the copy you need to remove these two lines; the second one will block breakpoints from working in the method:

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler", " 1.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

@MikeHillberg MikeHillberg added the team-Markup Issue for the Markup team label May 6, 2022
@andrewittorio
Copy link
Author

Hi Mike,

Thank you for your answer.

I was meaning that if I set a breakpoint on the first line of my Main method, the breakpoint is not fired. So as the app runs, I suppose that the auto generated code is called.

I can also add that I did not copy the main from the auto generated code, but I created it manually as explained in the sample.

Thank you,

@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@applefanbois
Copy link

applefanbois commented Dec 29, 2022

I modified my WinUI3 project by adding the DISABLE_XAML_GENERATED_MAIN and the creating the Program.cs. Everything is firing normally, but while the UI is being created the execution eventually stop, never on the same spot. I trace with F10 step by step and eventually it won't go to the next line, the cpu is zero, the code still answer to resize window, but the app is not displaying anything. Never seen such a thing in my entire life. How is it possible that a main thread will simply not continue to the next instruction and never the same one without any errors.

I have many await on the main thread that are interrupted by other code running on the main thread. So the multi-threading is completely broken now. The scary part is that they never return to what they were doing. This is technically impossible, but with MS everything is possible.

@applefanbois
Copy link

applefanbois commented Feb 6, 2023

I just want to say that the WinUI3 single-instance sample project has a huge problem and that will make it useless for most people because everything useful will not work. Writable bitmap will never initialize with content and Webview2 will not start ever. The problematic lines of codes were right in the beginning.

static async Task Main(string[] args) //This will break everything using COM
static int Main(string[] args) //This is the solution to make everything work normally.

I don't know why making the main an async Task would break everything, but it does.

"await writeableBitmap.SetSourceAsync(filestream)" would hang and never complete and
"await webView2.EnsureCoreWebView2Async();" would throw or fail

So the solution was to replace the Main function definition with "static int Main(string[] args)"

@github-actions
Copy link

github-actions bot commented Aug 7, 2023

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants