我正在开发一个UWP桌面桥应用程序。我已经创建了打包项目,并为sideloading.When创建了一个应用程序包。单击应用程序图标一次,应用程序就会启动successfully.But,双击图标,应用程序就会崩溃。
我已经按照以下链接创建了打包项目: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" />
有可能避免崩溃问题吗?
发布于 2019-05-31 16:07:41
请检查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();
}
https://stackoverflow.com/questions/56373081
复制相似问题