C#将参数传递给已经运行的进程 [英] C# Passing Arguments to already running process

查看:436
本文介绍了C#将参数传递给已经运行的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在研究一个程序,我需要将参数传递给一个进程(process.startinfo.arguments),但问题是该进程已经在运行。我该怎么做呢?谢谢!

Hello. I'm working on a program and I need to pass arguments to a process (process.startinfo.arguments), but the problem is that the process is already running. How do I acomplish this? Thanks!

推荐答案

您不能使用命令行参数对任意已经运行的进程执行此操作。该过程已经启动,并且此时已经获得并处理了这些参数。改变它的行为为时已晚。



但是,你可以用一些特殊的方式设计你的过程来实现非常相似的东西。此过程应将 与同一应用程序的第二个实例进行通信,这将是一个单独的过程 。使用IPC,您的第二个实例应该与第一个实例通信并传递所需的数据(例如,取自您在第二次运行时提供的不同命令行参数)。您的第一个实例应该监听此类连接和数据并相应地修改其行为。



实际上,我为这种通信设计了一个非常优雅的实用程序,它也可以用于实现应用程序的单进程行为。我在过去的答案中描述了它:

自定义Windows右键单击命令启动多个实例 [ ^ ],

仅向一个流程实例输入多个命令 [ ^ ]。



实际上,第二个问题的作者与你的想法有同样的想法;我回答了这个问题。



-SA
You cannot do it to an arbitrary already running process using command-line arguments. The process has been started already and those arguments are already obtained and processed at this moment. It's too late to change its behavior.

However, you can design your process in some special way to implement something pretty similar. This process should communicate with second instance of the same application, which would be a separate process. Using IPC, your second instance should communicate with the first instance and pass required data (say, taken from different command-line arguments you supplied on second run). Your first instance should listen to such connection and data and modify its behavior accordingly.

Actually, I designed a pretty elegant utility for such communications, which also can be used for implementation of single-process behavior of the application. I described it in my past answers:
Custom Windows right-click command launching multiple instances[^],
Enter multiple commands to only one process instance[^].

Actually, the author of the second question got to the same idea as yours; and I answered to that question.

—SA


这篇关于C#将参数传递给已经运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆