Program.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Diagnostics;
 6  
 7  using Microsoft.Extensions.Configuration;
 8  using Microsoft.Extensions.DependencyInjection;
 9  using Microsoft.Extensions.Hosting;
10  
11  namespace MouseWithoutBordersService
12  {
13      public delegate void StopService();
14  
15  #pragma warning disable SA1402 // File may only contain a single type
16  #pragma warning disable SA1649 // File name should match first type name
17      public static class CmdArgs
18  #pragma warning restore SA1649 // File name should match first type name
19  #pragma warning restore SA1402 // File may only contain a single type
20      {
21          public static string[] Value { get; set; }
22  
23          public static StopService StopServiceDelegate { get; set; }
24      }
25  
26      internal sealed class Program
27      {
28          [STAThread]
29          private static void Main()
30          {
31              if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMouseWithoutBordersEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled
32                  || PowerToys.GPOWrapper.GPOWrapper.GetConfiguredMwbAllowServiceModeValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled )
33              {
34                  // TODO: Add logging.
35                  // Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
36                  return;
37              }
38  
39              string[] args = Environment.GetCommandLineArgs();
40              CmdArgs.Value = args;
41              var builder = Host.CreateDefaultBuilder(args);
42  
43              var host = builder
44              .UseWindowsService(options =>
45              {
46                  options.ServiceName = "PowerToys.MWB.Service";
47              })
48              .ConfigureServices(services =>
49              {
50                  services.AddHostedService<Worker>();
51              })
52              .Build();
53  
54              // These warning are disabled because StreamJsonRpc brings in thread analyzers to the project. Enable warnings once we adopt thread analyzers globally.
55  #pragma warning disable VSTHRD101 // Avoid unsupported async delegates
56  #pragma warning disable VSTHRD110 // Observe result of async calls
57              CmdArgs.StopServiceDelegate = async () => { await host.StopAsync(); };
58              host.Run();
59              host.StopAsync();
60  #pragma warning restore VSTHRD110 // Observe result of async calls
61  #pragma warning restore VSTHRD101 // Avoid unsupported async delegates
62          }
63      }
64  }