简体   繁体   English

双击时,UWP Desktop Bridge应用程序崩溃

[英]UWP Desktop Bridge app crashes on double click

I'm working on a UWP Desktop Bridge App. 我正在开发一个UWP桌面桥应用程序。 I have created the packaging project and created an app package for sideloading.When I click on the App Icon once, the app launches successfully.But on double click of the Icon, the app crashes. 我已经创建了打包项目并创建了一个用于sideloading的应用程序包。当我点击应用程序图标一次时,应用程序启动成功。但是双击图标,应用程序崩溃了。

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. 我在链接后创建了打包项目: https//docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-packaging-dot-net该应用程序正常运行单击应用程序图标 Is it because on double click, the .exe is being invoked twice and that's the reason for the crash? 是因为双击,.exe被调用两次,这是崩溃的原因?

Here is the Main method of the Background process 这是后台进程的Main方法

   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)
        {
        }
    }

The code to call: 要调用的代码:

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

Also,inside package manifest: 另外,内包清单:

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

and

<rescap:Capability Name="runFullTrust" />

Is it possible to avoid the crash issue? 是否可以避免崩溃问题?

if it's a connection, if you try to connect it twice, it may be crashed. 如果是连接,如果你尝试连接两次,它可能会崩溃。 you can make u app only start up once, if it started, then switch to started process. 你可以让你的应用程序只启动一次,如果它启动,然后切换到启动过程。

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

   }

Please check the AppService Bridge Sample . 请检查AppService Bridge示例 It creates and starts a separate thread each time to create a new AppServiceConnection instance and call its OpenAsync method. 它每次创建并启动一个单独的线程来创建一个新的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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM