在单独的线程上执行程序
我正在尝试与第三方应用程序交互,但每当我尝试调用方法时,都会收到一条错误消息:System.InvalidOperationException:操作必须在应用程序线程上执行
。异常的类型为 System.Reflection.TargetInitationException ,我猜测这是因为我的应用程序在完全独立的进程中运行。有没有办法让我的程序(控制台应用程序)开始在与第三方应用程序相同的线程上运行?
I'm trying to interact with a third party application but whenever I try and call a method, I get an error message saying System.InvalidOperationException: Operation must be performed on the application thread
. The exception is of type System.Reflection.TargetInvocationException
and I'm guessing this is because my app is running in a completely separate process. Is there a way to get my program, which is a console application, to start running on the same thread as the third party app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法让两个单独的应用程序在单个线程中运行。
如果您尝试与应用程序中的某些 UI 控件进行交互,可以使用
.Invoke()
来确保相关代码在 UI 线程中执行。如果您尝试操纵第三方应用程序,适当的解决方案完全取决于您与其交互的方式。它提供API吗?向其窗口句柄发送消息? ??
为了与另一个应用程序的正在运行的实例进行交互,您需要使用远程处理(已过时)或 WCF 之类的东西。本质上,另一个应用程序充当服务器,您必须以某种方式连接到它。引用其程序集、实例化其中定义的类之一并调用该实例上的方法不会让您与已经执行的进程进行对话。
You can't get two separate applications to run in a single thread.
If you are trying to interact with some UI controls in your application, you can use
.Invoke()
to make sure the relevant code is executed in the UI thread.If you're trying to manipulate the a third party application, the appropriate solution depends entirely on how you're interacting with it. Does it provide an API? Sending messages to its window handle? ??
In order to interact with a running instance of another application, you'll need to use something like remoting (obsolete) or WCF. Essentially, the other application acts as a server, and you have to somehow connect to it. Referencing its assembly, instantiating one of the classes therein defined and calling methods on that instance won't let you talk to an already-executing process.