tags:

views:

598

answers:

4

Is there a way to call method inside a running .NET assembly.

The problem is that I have a .NET application that is run on system startup and puts itself in the system tray. The user can open the application dialog by double-clicking on the tray icon but I want to show the form even when the user start the application again (and its already running). Currently in the main method, I get the running .NET assembly as Process but dont know how to get a reference to the internal class/methods.

I am able to get whether an instance is already running but want to know how to call methods from the process handle that I have. When its the second instance, I check all the running processes and get a pointer to my already running process. I was wondering if there is a way in .NET (thorough reflection or something else) that will use this process handle and return me a pointer to an interface.

-Added 7-Apr-09- There are a number of solution to find whether an instance is already running but what I really want is to cast the process into some interface and call a method on it. OR Pass a message into the running application with some parameters.

+1  A: 

Not without using some kind of inter process communications mechanism. One relatively straight forward way to implement such an IPC mechansim in .Net is to use WCF and expose the interface you need in a WCF endpoint.

There are other IPC mechanisms you could use including named pipes, sockets, etc. but in those cases you have to worry about your data protocols and serialisation. The advantage with WCF is all that is taken care of for you.

sipwiz
A: 

IMO, what would be better for you is to limit your application to a single Instance using Mutex which could set a flag and on the start of application you can re-check that flag to see if the application is already running or not.

Aamir
That will prevent a second instance, but how then do you propose to activate the first instance?
Jim Mischel
+5  A: 

This is not really the solution of the question (calling a specific method) but it could solve your problem of opening the form.

The solution is too complex to copy it here so check it out : http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx

What does this code do ?

  1. When the user tries to open the application a second time, a second instance does not open on the screen.
  2. Instead the first instance comes to the foreground. If it is minimized, it is restored.
  3. If the application is minimized to the SYSTEM TRAY, then it is restored from there.

You may even jump to the end of the page if you need to pass parameters between instances (I really like the VB solution... It's sad that it is not designed for any language. But you are still able to use it by referencing the VisualBasic namespace in your C# app.)

Hope this helps.

Julien N
I agree with you about the VB method being the preferred solution. You can use it in C#, but it's a little bit inconvenient.
Jim Mischel
A: 

The best solution to do so is to declare a global Mutex

            try
            {
                System.Threading.Mutex.OpenExisting("mymutex");
                //MessageBox.Show("already exist instance");
                //TODO

            }
            catch
            {
                m = new System.Threading.Mutex(true, "mymutex");

            }
Ahmed Said
But how does that activate the pre-existing process?
Jim Mischel
This will not activate thing, it only checks if an instance already loaded or not
Ahmed Said