title: How to access a running .net application
body: <p>the .net Windows Form application we developed (vb.net but c# answer is ok too) has some APIs (edit: yes, our own) to allow users to automate some tasks. 
  Everything is fine when the application is started through APIs by, say, Visual Studio. What we cannot get to work though is to assign an already running instance of our application to a new application object in visual studio. 
  We have seen there are some methods available for COM objects (getojbect) to access a running instance of an application but how about .net applications?</p>
  
  <p>Rephrasing the question, we would like that, when a user calls the New() constructor of our application, the new object points to the running instance of our application (if any) instead of trying to create a new one (which is not possible by the way because we have made it single instance by checking through Mutex that no other instance of our application is running).</p>
  
  <p>EDIT:
  Sample code in the user application to automate some tasks</p>
  
  <pre><code>Imports TheApplication
  Public Class WinFormByUser
      Private ApplicationObject As TheApplication.MainForm
      Public Sub OpenTheApplication()
          ApplicationObject = New TheApplication.MainForm
          Rem here theapplication should create a new instance if no instance of TheApplication is running. BUT, if an instance of the application 
          Rem is already running (in a different process, maybe started directly from the user), the ApplicationObject should point to the running 
          Rem instance from now on, instead of trying to create a new instance
          ApplicationObject.DoSomething()
      End Sub
   End Class
  </code></pre>
  
  <p>Sample code inside TheApplication</p>
  
  <pre><code>Imports System.Threading
  Public Class MainForm
  Private ApplicationOpenedThroughAPI As Boolean = False
  Private Shared mtx As Mutex
  Private firstInstance As Boolean = False
  Dim AppName As String = "TheApplicationName"
  Public Sub New()
      If Application.ProductName.ToString() &lt;&gt; AppName Then
          Rem if TheApplication is opened externally through API the name is different therefore we can determine the boolean value
          ApplicationOpenedThroughAPI = True
      End If
      mtx = New Mutex(True, AppName, firstInstance)
      If firstInstance Then
          InitializeComponent()
          DoAllTheNecessaryStuff()
      Else
          If ApplicationOpenedThroughAPI = False Then
              MsgBox("Application is running, can't open second instance")
          Else
              ReturnTheRunningInstance()
          End If
      End If
  End Sub
  Private Sub ReturnTheRunningInstance()
      Rem please help here. what to do?
  End Sub
  Public Sub DoSomething()
      Rem this does something and can be called by API user
  End Sub
  End Class
  </code></pre>
  
  <p>Please note that the solution could either be adding some code inside the application in the Sub ReturnTheRunningInstance() or in the user code, maybe checking if the application is running through something like Process.GetProcessesByName("TheApplicationName").Length and then do something in case.</p>
  
  <p>Thanks!</p>