是否可以从另一个c#控制台应用程序控制WinForm?

时间:2011-03-08 08:32:16

标签: c# .net winforms webbrowser-control watin

我是2天的.NET C#编码器试图从 ProjectB - C#控制台应用程序控制 ProjectA - WinForm

基本上我正在使用WatiN在ProjectA中的WebBrowser控件中自动执行测试。

当我运行执行winformWithWebBrowserTest.exe的ProjectB时,带有webbrowser的winform显示出来。但后来它无法访问form1。如何从ProjectB ???

访问webbrowser控件

错误:

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();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

你不能这样做,因为这两个进程都在它们各自的进程空间中运行。你需要重新进行2天内不建议的进程间通信:)。