如何在新流程中启动第二个项目的新实例
我希望将单独的类库项目中定义的窗口(WPF)显示为新的单独进程。有什么办法可以做到这一点吗?
我需要在新进程中启动第二个项目实例,因为当我通过这种方式启动它时,仍然会占用内存:
secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();
我有一个包含多个项目的解决方案。
- StartUp项目是WPF应用程序,输出类型:Windows应用程序(exe文件)。
- 所有其他项目都是WFP应用程序,输出类型:类库(dll文件)。
现在我正在通过以下代码运行“应用程序”(此解决方案中构建为 dll 的其他项目):
secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();
我想要在新进程中运行该应用程序...通常我会使用 Process.Start()
,但我不能在这种情况下,因为它需要 exe 文件作为基础,而且我有(并且想要)DLL。
I'm looking to show window (WPF) that is defined in separate class library project as new separate process. Is there any way to do this?
I need to start the second project instance in new process, because there stay occupied memory when I start it by this way:
secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();
I have ONE solution with multiple projects.
- StartUp project is WPF app., output type: Windows application (exe file).
- All other projects are WFP app., output type: Class library (dll file).
Now I am running "applications" (other projects in this one solution built as dll) by this code:
secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();
I want is runnig that apps in new process... Normally I would use Process.Start()
, but I can't is this case because it needs exe file as agrument and I have (and want) DLLs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将命令行参数传递给主 .exe,告诉它要运行哪个“子应用程序”。然后主 .exe 可以在新进程中启动自身并告诉新实例要运行哪个子应用程序。例如,在主 .exe 应用程序中,将这样的逻辑放入您的 Application 类中:
然后,只要您想在新进程中启动子应用程序之一,您就可以这样做:
You could pass command line arguments to the main .exe to tell it which of the 'sub-apps' to run. Then the main .exe can just launch itself in a new process and tell the new instance which sub-app to run. For example in the main .exe app, put logic like this in your Application class:
Then anytime you want to launch one of the sub-app in a new process, you can do so like this:
假设您有一个包含 2 个项目的解决方案 - 一个项目编译为应用程序 (EXE),而第二个项目编译为类库 (DLL)。我假设 DLL 已定义某种类型(例如窗口),您希望从 EXE 启动该类型。
最简单的方法是添加对 DLL 的引用。在解决方案资源管理器中右键单击 EXE 项目,然后选择添加引用...。对话框打开时请稍等一下。从项目选项卡中,选择 DLL 项目。单击“确定”。
现在,在您的 EXE 项目中,
WPFWindow
将作为导入类型提供。您需要将using secondaryProject;
添加到使用
WPFWindow
的每个代码文件的顶部。我通常使用键盘快捷键 CTRL + 句点自动执行此操作。我所描述的方法是在 C# 中使用 DLL 的标准方法。您可以手动加载它们,但这有点复杂,而且可能不是您想要做的。
编辑:
阿列克谢是正确的。我认为我们遇到的是 XY 问题。您尝试做的事情可能非常简单,但方法(实例化 DLL 中定义的窗口)却不然。
请注意,您运行的任何代码(在您的情况下,您的
WPFWindow
)都必须源自应用程序,即使代码本身是在 DLL 中定义的。 DLL 本身通常不向操作系统提供有关如何运行其中包含的任何代码的信息。考虑添加另一个运行您使用
Process
调用的WPFWindow
的 EXE 项目。不过,这个建议可能是错误的,因为我们仍然不知道您的最终目标是什么。你问“我怎样才能像小鸟一样拍打翅膀?”当正确的问题可能是“我如何购买机票?”Let's assume you have a solution with 2 projects - one project compiles to an application (EXE), while the second compiles to a class library (DLL). I'll assume that the DLL has some type defined (say, a window) which you want to start from the EXE.
The simplest way to do this, is simply add a reference to the DLL. Right-click on the EXE project in the Solution Explorer, and choose Add Reference.... Wait a minute while the dialog opens. From the Projects tab, select the DLL project. Click OK.
Now, in your EXE project,
WPFWindow
will available as an imported type. You need to add ausing secondProject;
to the top of each code file that uses
WPFWindow
. I normally do this automatically using the keyboard shortcut CTRL + Period.The method I've described is the standard method of using DLLs in C#. You can load them manually, but that is a little more complex, and likely isn't what you want to do.
Edit:
Alexei is correct. I think what we have here is an XY Problem. What you're trying to do is probably very easy, but the approach (instantiating a window defined in a DLL) is not.
Be aware that any code you run (in your case, your
WPFWindow
) has to originate from an application, even though the code itself is defined in a DLL. A DLL by itself normally provides no information to the operating system about how to run any of the code contained within.Consider adding another EXE project which runs your
WPFWindow
which you call usingProcess
. This suggestion might be wrong, though, as we still don't know what your end goal is. You're asking "How do I flap my wings like a bird?" when the correct question could be "How do I buy a plane ticket?"