将应用程序作为线程而不是进程运行
不久前,我在这里问了一个关于如何在另一个应用程序的内存空间中作为线程运行应用程序的问题,而不是一个新进程:将程序作为线程而不是进程执行
作为该问题的后续,我想我有关于我如何做到这一点以实现我正在尝试做的事情的理论。首先,让我说,我这样做的原因是因为我试图创建一个测试台应用程序,可以“测试”其他应用程序,但控制它们,以便如果它们出现异常,我的测试台应用程序可以结束线程并自动重新启动。
我关于如何做到这一点的理论是创建一个接口,例如名为 ITestBed 的接口,并让我的应用程序中的主类实现它。该实现将包含一种方法,例如 TestApp()。我需要从我的测试床应用程序做的就是调用这个方法,并且应用程序端的这个方法可以镜像我当前对象的构造函数?这行得通吗?
本质上,我想做的是模仿 Visual Studio 的工作方式。如果您在程序中设置断点,点击在 Visual Studio 中运行您的应用程序,您的应用程序将作为 Visual Studio 应用程序的子应用程序运行。要测试这一点,您可以结束 Visual Studio 应用程序,您的应用程序也将结束。我怎样才能做到这一点?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要将其作为单独的线程运行。
当您使用 Process.Start 启动时一个进程,它是当前应用程序的子进程。
只需注意 Process.Exited 事件(确保将 EnableRaisingEvents 设置为 true ),并用它来查看进程何时退出。然后您可以在此时自动重新启动该过程。
这会起作用。您可以通过反射加载主exe的程序集,并构造主对象,然后调用您的方法。然而,这样做会使该进程成为当前应用程序的一部分 - 因此它会产生一些副作用。在这种情况下,您正在共享进程内存空间。
如果您这样做,您可能需要研究应用程序域。通过在单独的 AppDomain 中加载并“启动”ITestBed 实现,您将保护您的主应用程序。
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.
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.