2

I've been able to use a winforms application to open another winforms application using:

Rhino4.Application oRhino = (Rhino4.Application)Activator.CreateInstance(Type.GetTypeFromProgID("Rhino4.Application"));

But how do I check if it gets closed? Is it possible to create an event that will get fired when the user closes the application?

EDIT

Process[] pr = Process.GetProcessesByName("Rhino4");


                for (int i = 0; i < pr.Length; i++)
                {


                    if (pr[i].MainWindowTitle != null)
                    {
                        if (pr[i].MainWindowTitle.Length > 4)
                        {
                            if (pr[i].MainWindowTitle.Substring(0, 4) == "wall")
                            {
                                pr[i].Exited += new EventHandler(caseInfoMenu_Exited);

                            }
                        }
                    }

                }


void caseInfoMenu_Exited(object sender, EventArgs e)
        {
            MessageBox.Show("Window closed");
        }

I've managed to identify the process using this code. But the Exited-event doesn't fire when I close the program.

Bildsoe
  • 1,310
  • 6
  • 31
  • 44

3 Answers3

3

Its maybe not the most elegant solution but indirectly you could do this by checking if the process exist or not and then do this repeatable. This is of course if you do not already have a handle to the process.

void checkProcess()
{
    Process[] processes = Process.GetProcessesByName("NameOfProcess");
    if (processes.Length == 0)
    {
         // No such process
    }
    else
    {
         foreach (Process proc in processes)
         {
            // do something with proc
         }
    }
}

Edit: Some thoughts on this after reading the posts in Abdul's answer plus your own question. This is by no means an answer, but maybe it can help you on your quest.

Firstly, Activator.CreateInstance calls the best fitting constructor on the object type that you give to it and returns a handle to that object. It does create the threads/processes itself and thus it has not knowledge about them. The (9) processes you'll see in your list are probably created by the Rheno4 class itself. There is a discussion about this here.

Secondly, according to msdn the EnableRaisingEvents property should be set to true when the process is created for the Exited event to function correctly. This leaves me wondering what happens when you attach the event after the process is already created?

You could of course iterate over all matching processess before and after calling CreateInstance to extract all new instances of Rheno4 that has been created. But this is far from a bulletproof solution and the risk is that you are fetching processes that are created by someone else or that not all processes are retreived (in case there is a delay in creating the other object). Depending on your needs, however, this maybe is appliable.

Another thought. The processes returned from the GetProcessesByName has a rich set of properties. Maybe you can look though these and find a common denominator for the processes returned. The ones I would start to investigate are: Threads, StartInfo, MainModule.

Avada Kedavra
  • 8,523
  • 5
  • 32
  • 48
  • von Plan - This has given me a lot to work with. Thanks. I'm gonna look into the properties, I think you're right. One of these must contain some information which I can use. – Bildsoe Jun 15 '11 at 07:59
2

What about catching Exited event

myProcess.Exited += new EventHandler(myProcess_Exited);



private void myProcess_Exited(object sender, System.EventArgs e)
    {

        eventHandled = true;
        Console.WriteLine("Exit time:    {0}\r\n" +
            "Exit code:    {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
    }

Source msdn.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38
  • @Abdul Muqtadir - how do I get the process from the Application. The reason why I use Activator.CreateInstance is because I need to control the application. I start a script after opening the application and need to know if the user closes it. – Bildsoe Jun 14 '11 at 12:30
  • Can't get you, can't you launch the process? – FIre Panda Jun 14 '11 at 12:33
  • Nope, cause then I have no control of the lauched application. But perhaps i can combine the Exited-event you mention with Process.GetProcessByName? – Bildsoe Jun 14 '11 at 12:36
  • @Bildsoe: Look at the code in the MSDN example Abdul provided. If you start the application in code and you can save a handle to the process, then I think you should go for this approach as it gives you the most control of the process. If you do not posess control of the process (ie, its started by windows/user/etc) then I would recommend the GetProcessByName approach, i use it myself and I belive it to be rock solid. I see no point in mixing the two of them. – Avada Kedavra Jun 14 '11 at 13:04
  • @Tele von Plan - My problem is that I need to create the application instance by application.createInstance inorder to access the scripting module of the application. How will I get the handle of the current process? When I try to access process.handle access is denied. Do I use mainWindowTitle then? cause I tried this, and it returned nothing. – Bildsoe Jun 14 '11 at 14:15
  • @Tele von Plan - I tried using processName, but 9 processes has the same name as the one I'm trying to reference. – Bildsoe Jun 14 '11 at 14:21
  • Hmmm - I'm kinda stuck. I found that the Exited-event applied to the process identified by the windowMainTitle, didn't fire, if I however applied the event to all 9 running process, it does fire. But how will is it possible to control, that it is the correct window which has been closed ie. if there are more instances of the same application? – Bildsoe Jun 14 '11 at 15:03
0

If you want to start the application again after closing then :- I think you need to crearte a Windows Service which will keep checking the process running and if it is closed then start the application again

As far as events are concern then "Closing" and "Close" events are there in Windows App which fires when user shutdowns the app.

Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • If you create a windows service of this kind, be aware that you cannot easily start gui applications as a system service. It is possible, but not always recommended, see http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application for more infor on the subject. – Avada Kedavra Jun 14 '11 at 12:38