问题描述
我是 2 天大的 .NET C# 编码员,试图从 ProjectB - C# 控制台应用程序控制 ProjectA - WinForm.
I'm 2 days old .NET C# coder trying to get control on ProjectA - WinForm from ProjectB - C# console app.
基本上,我使用 WatiN 在 ProjectA 中的 WebBrowser 控件中自动进行测试.
Basically I'm using WatiN to automate test within WebBrowser control in ProjectA.
当我运行执行 winformWithWebBrowserTest.exe 的 ProjectB 时,显示带有 webbrowser 的 winform.但是随后它无法访问form1.如何从 ProjectB 访问 webbrowser 控件???
When I run ProjectB which executes winformWithWebBrowserTest.exe, the winform with webbrowser shows up. But then it fails to access form1. How can I access the webbrowser control from ProjectB ???
错误:
System.Runtime.IteropServices.InvalidComObjectException
COM
object that has been separated from its underlying RCW cannot be used
ProjectA WinForm: (winformWithWebBrowserTest.exe)
namespace WindowsFormsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
}//end class
}//end namespace
项目 B 控制台应用:(WatinConsoleExample.cs)
namespace ConsoleApplication1
{
class WatinConsoleExample
{
[STAThread]
static void Main(string[] args)
{
//run ProjectA exe
System.Diagnostics.Process Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "C:\\Users\\m-takayashiki\\Documents\\Visual Studio 2010\\Projects\\winformWithWebBrowserTest\\winformWithWebBrowserTest\\bin\\Release\\winformWithWebBrowserTest.exe";
Proc.Start();
WindowsFormsApplication1.Form1 form1 = new Form1();
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
//exception occurs below ..........
var ie = new IE(form1.webBrowser1.ActiveXInstance);
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByClass("lst")).TypeText("this is awesome!!");
ie.Button(Find.ByName("btnG")).Click();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
}
推荐答案
您不能这样做,因为两个进程都在各自的进程空间中运行您需要进行进程间通信,这对于 2 天大的人来说是不推荐的:)
You cannot do it , since both processes are running in their seperate process space & you need to go over interprocess communication which is not recommended for a 2 days old:).
这篇关于是否可以从另一个 c# 控制台应用程序控制 WinForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!