views:

86

answers:

1

A while back, I asked a question on here about how to run an application as a thread in the memory space of another application, rather then a new process: http://stackoverflow.com/questions/3169234/executing-a-program-as-a-thread-and-not-as-a-process

As a follow up to that question, I think I have a theory on how I might do this to acheive what it is I'm trying to do. First off, let me say, the reason I am doing this is because I am trying to create a test bed application that can "test" other applications, but control them so if they get an exception, my test bed application can end the thread and restrart it automatically.

My theory on how to do this is to create an interface, called ITestBed for example and have the main class in my application implement this. The implementation will contain one method, TestApp(), for example. All I need to do from my test bed application is call this method and this method on the application side can just mirror the constructor of my current object? Will this work?

Essentially, what I am trying to do is mimmick how Visual Studio works. If you set a break point in your program, hit to run your application in Visual Studio, your application is running as a child of the Visual Studio application. To test this, you could end the visual studio application and your application will also end. How can I acheive this?

+1  A: 

You don't need to run it as a separate thread.

When you use Process.Start to launch a process, it is a child process of the current application.

Just watch for the Process.Exited event (making sure to set EnableRaisingEvents to true), and use it to see when the process exits. You can then restart the process automatically at that point.


My theory on how to do this is to create an interface, called ITestBed for example and have the main class in my application implement this. The implementation will contain one method, TestApp(), for example. All I need to do from my test bed application is call this method and this method on the application side can just mirror the constructor of my current object? Will this work?

This will work. You can load the main exe's assembly via reflection, and construct the main object, then call your method. Doing this makes the process part of your current application, however - so it has some side effects. You're sharing process memory space in this situation.

If you do this, you may want to look into Application Domains. By loading and "launching" your ITestBed implementation in a separate AppDomain, you'll protect your main application.

Reed Copsey
To follow up on Reed's answer: it's pretty easy to write an application that will mess up the entire process it's in (even if you isolate it into an AppDomain). The *process* is the proper scope of work for things like this. Visual Studio uses child processes - not threads - for debugging.
Stephen Cleary