3

I'm playing with Microsoft's UWP AppServiceBridgeSample (here).

It is working well, but I would like to get rid of the console window of the BackgroundProcess application. The reason for this is that my BackgroundProcess starts another Win32 desktop application and works only as a mediator, so I don't want to disturb users with a console window. (Yes, it can be minimized, but I would rather not show it at all).

I have tried to hide it using the API mentioned here, but with no luck, the console window is still visible. Neither did switching the project's output type from Console Application to Windows Application.work.

Another thing I have tried was to create other BackgroundProcess project as a Windows application. It runs fine until I call AppServiceConnection.OpenAsync(), which causes the BackgroundProcess application to exitstrong text, thus the connection to UWA is not available.

static async void ThreadProc()
{
  try
  {
     AppServiceConnection connection = new AppServiceConnection();
     connection.AppServiceName = "CommunicationService";
     connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
     connection.RequestReceived += Connection_RequestReceived;
     AppServiceConnectionStatus status = await connection.OpenAsync();
     //status check etc. ...
  }
  catch(Exception ex)
  {
     MessageBox.Show(ex.ToString());
  }
}

It seems that opening the AppService connection is only possible from a console app.

So here are my two questions:

  1. Is it, by any chance, even possible to hide the background process' console window?
  2. Can I use the background process as a Windows application, without AppServiceConnection failing during OpenAsync calls?
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
kibitzerCZ
  • 487
  • 8
  • 20

1 Answers1

5

Re 1: Go into the project settings and change the output type from Console to Windows app. Also make sure the Main() function doesn't exit until you are done with the background process. Here is a better sample that shows this with a Windows Application: https://stefanwick.com/2017/05/26/uwp-calling-office-interop-apis/

enter image description here

Re 2: AppServiceConnection works the same way from a windowed application as well. Make sure you add the right reference to the Windows.winmd to be able to build. If you have trouble with that, please post a specific question with details of the problem you are seeing

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
  • I already tried both ways, as stated in my question. Unfortunately when built as Window Application it runs fine until I call AppServiceConnection.OpenAsync(), which causes the BackgroundProcess application to exit (without exception). I am sure all references are in place, because when I switch the project to Console Application, it works as expected. – kibitzerCZ Jun 02 '17 at 13:02
  • 2
    You also need to make sure not to exit Main() until you are done using the process. I have updated my answer accordingly and pointed you to a better sample project for this. – Stefan Wick MSFT Jun 02 '17 at 14:58
  • Oh snap, I have been missing that the connection runs in a standalone thread - now it's fairly clear, that the main thread is exiting during await! Thanks a lot for your assistance and your sample, which gave me all I need. – kibitzerCZ Jun 05 '17 at 08:02