UWP Desktop Bridge应用程序双击崩溃

时间:2019-05-30 06:45:24

标签: c# xaml uwp desktop-bridge

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

我通过以下链接创建了包装项目: https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-packaging-dot-net 单击该应用程序图标即可正常运行该应用程序。是因为双击时.exe被调用了两次,这就是崩溃的原因吗?

这是后台进程的主要方法

   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" />

是否可以避免崩溃问题?

2 个答案:

答案 0 :(得分:0)

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

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);

   }

答案 1 :(得分:0)

请检查AppService Bridge Sample。每次创建并启动一个单独的线程来创建新的AppServiceConnection实例并调用其OpenAsync方法。

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();
}