title: C# dot net core single instance app passing parameters to first instance
body: <p>Recently I decided to migrate one of my WPF Windows desktop apps written in C# and targeting <strong>.NET Framework 4.5 to</strong> the newest <strong>.NET Core 3.1</strong>. All was good until I had to add support for single instance application while being able to pass any parameters from the second instance to the first running instance. My previous WPF implementation for single instance application was using <strong>System.Runtime.Remoting which is not available in .NET Core</strong>. Therefore I had to do something new. Below is the implementation that I came up with. It works great but I feel it can be improved. Please, feel free to discuss and improve the proposed solution.</p>
  <p>I created a SingleInstanceService which is using a semaphore to signal if it is the first instance or not. If it is the first instance, I create a TcpListener and I wait indefinitely for any arguments passed from a second instance. If a second instance is started, then I send the parameters of the second instance to the first listening instance and then I exit the second instance.</p>
  <pre><code>internal class SingleInstanceService
  {
      internal Action&lt;string[]&gt; OnArgumentsReceived;
  
      internal bool IsFirstInstance()
      {
          if (Semaphore.TryOpenExisting(semaphoreName, out semaphore))
          {
              Task.Run(() =&gt; { SendArguments(); Environment.Exit(0); });
              return false;
          }
          else
          {
              semaphore = new Semaphore(0, 1, semaphoreName);
              Task.Run(() =&gt; ListenForArguments());
              return true;
          }
      }
  
      private void ListenForArguments()
      {
          TcpListener tcpListener = new TcpListener(IPAddress.Parse(localHost), localPort);
          try
          {
              tcpListener.Start();
              while (true)
              {
                  TcpClient client = tcpListener.AcceptTcpClient();
                  Task.Run(() =&gt; ReceivedMessage(client));
              }
          }
          catch (SocketException ex)
          {
              Log.Error(ex);
              tcpListener.Stop();
          }
      }
  
      private void ReceivedMessage(TcpClient tcpClient)
      {
          try
          {
              using (NetworkStream networkStream = tcpClient?.GetStream())
              {
                  string data = null;
                  byte[] bytes = new byte[256];
                  int bytesCount;
                  while ((bytesCount = networkStream.Read(bytes, 0, bytes.Length)) != 0)
                  {
                      data += Encoding.UTF8.GetString(bytes, 0, bytesCount);
                  }
                  OnArgumentsReceived(data.Split(' '));
              }
          }
          catch (Exception ex)
          {
              Log.Error(ex);
          }
      }
  
      private void SendArguments()
      {
         try
         {
              using (TcpClient tcpClient = new TcpClient(localHost, localPort))
              {
                  using (NetworkStream networkStream = tcpClient.GetStream())
                  {
                      byte[] data = Encoding.UTF8.GetBytes(string.Join(&quot; &quot;, Environment.GetCommandLineArgs()));
                      networkStream.Write(data, 0, data.Length);                       
                  }
              }
          }
          catch (Exception ex)
          {
              Log.Error(ex);
          }
      }
  
      private Semaphore semaphore;
      private string semaphoreName = $&quot;Global\\{Environment.MachineName}-myAppName{Assembly.GetExecutingAssembly().GetName().Version}-sid{Process.GetCurrentProcess().SessionId}&quot;;
      private string localHost = &quot;127.0.0.1&quot;;
      private int localPort = 19191;
  }
  </code></pre>
  <p>Then in App.xaml.cs, I have the following code:</p>
  <pre><code>protected override void OnStartup(StartupEventArgs e)
  {            
     SingleInstanceService singleInstanceService = new SingleInstanceService();
  
     if (singleInstanceService.IsFirstInstance())
     {
         singleInstanceService.OnArgumentsReceived += OnArgumentsReceived;      
         // Some other calls
     }
     base.OnStartup(e);
  }
  
  private void OnArgumentsReceived(string[] args)
  {
      // ProcessCommandLineArgs(args);           
  }
  </code></pre>
  <p>Please feel free to discuss over this topic which I think is a very common use case among Windows desktop developers. Thank you.</p>