UWP Desktop Bridge应用程序双击崩溃

 收藏

我正在开发UWP Desktop Bridge应用程序。我已经创建了打包项目并创建了一个用于侧面加载的应用程序包。 图标一次,应用程序成功启动。但是,双击图标,应用程序将崩溃。

I have created the packaging project following the link : https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-packaging-dot-net The App runs normally on single click of the app icon. Is it because on double click, the .exe is being invoked twice and that's the reason for the crash?

这是后台过程的主要方法

   private static void Main(string[] args)
    {
        try
        {
            connection.AppServiceName = "CommunicationService";
            connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;

            // hook up the connection event handlers
            connection.ServiceClosed += Connection_ServiceClosed;
            connection.RequestReceived += Connection_RequestReceived;

            AppServiceConnectionStatus result = AppServiceConnectionStatus.Unknown;


            // static void Main cannot be async until C# 7.1, so put this on the thread pool
            Task.Run(async () =>
            {
                // open a connection to the UWP AppService
                result = await connection.OpenAsync();

            }).GetAwaiter().GetResult();

            if (result == AppServiceConnectionStatus.Success)
            {
                while (true)
                {

                }
            }
        }
        catch (Exception)
        {
        }
    }

调用代码:

    private async Task StartBackgroundProcess()
    {
        try
        {
            // Make sure the BackgroundProcess is in your AppX folder, if not rebuild the solution
            await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
        }
        catch (Exception)
        {
            MessageDialog dialog = new MessageDialog("Rebuild the solution and make sure the BackgroundProcess is in your AppX folder");
            await dialog.ShowAsync();
        }
    }

另外,内部包清单:

<desktop:Extension Category="windows.fullTrustProcess" Executable="BackgroundProcess.exe" />
    <uap:Extension Category="windows.appService">
      <uap:AppService Name="CommunicationService" />
    </uap:Extension>

<rescap:Capability Name="runFullTrust" />

是否可以避免崩溃问题?

回复
  • pnon 回复

    如果是连接,则尝试两次连接,则可能会崩溃。您可以使您的应用仅启动一次,如果已启动,则切换至已启动进程。

    public partial class App : Application
       {
           /// <summary>
           /// Application Entry Point.
           /// </summary>
           static Mutex muetx = new Mutex(true, "{666666666}");
    
           [System.STAThreadAttribute()]
           [System.Diagnostics.DebuggerNonUserCodeAttribute()]
           [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
           public static void Main()
           {
               if (muetx.WaitOne(TimeSpan.Zero, true))
               {
                   TestBeginInvoke.App app = new TestBeginInvoke.App();
                   app.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
                   app.Run();
               }
               else
               {
                   var pro = System.Diagnostics.Process.GetProcessesByName(nameof(TestBeginInvoke));
                   var handle = pro.FirstOrDefault().MainWindowHandle;
                   ShowWindow(handle, 1);
                   ShowWindow(handle, 9);
                   SetForegroundWindow(handle);
               }
           }
    
           [DllImport("USER32.DLL")]
           public static extern bool SetForegroundWindow(IntPtr hWnd);
    
           [DllImport("user32.dll")]
           private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    
       }
    

  • w无意 回复

    Please check the AppService Bridge Sample. It creates and starts a separate thread each time to create a new AppServiceConnection instance and call its OpenAsync method.

    static void Main(string[] args)
    {
        Thread appServiceThread = new Thread(new ThreadStart(ThreadProc));
        appServiceThread.Start();
    }
    
    static async void ThreadProc()
    {
        connection = new AppServiceConnection();
        connection.AppServiceName = "CommunicationService";
        connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
        connection.RequestReceived += Connection_RequestReceived;
        AppServiceConnectionStatus status = await connection.OpenAsync();
    }