开发者

Running an application as a thread and not a process

开发者 https://www.devze.com 2023-01-07 11:31 出处:网络
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: Executing a program as a thread and not as a

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: 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 meth开发者_StackOverflowod 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消