【问题标题】:Is it possible to have control on WinForm from another c# console app?是否可以从另一个 c# 控制台应用程序控制 WinForm?
【发布时间】:2011-03-08 08:32:16
【问题描述】:

我是 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

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

        }
    }
}

【问题讨论】:

  • 您不只是直接在浏览器中运行网站,而是使用众多浏览器自动化工具之一来驱动它,例如 watin 或 selenium,是否有特殊原因?
  • 我的项目是使用 Selenium 在 .NET 中启用针对 webbrowser 的自动化,但似乎 WatiN 是要走的路。

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


【解决方案1】:

你不能这样做,因为两个进程都在各自的进程空间中运行,你需要检查进程间通信,这不建议在 2 天前使用:)。

【讨论】:

  • 是否有替代方案,以便我可以运行弹出 winform 的 .exe(从 c# 构建)并让我单独的 c# 代码针对它运行 WatiN?
  • 可能有一些替代方案,但我不知道有什么,向你道歉。
  • 嗯窗口处理程序怎么样?也许那会起作用..我必须测试一下。
  • Visual Studio 使用了一种机制,当您调试程序时,它会以编程方式创建一个进程并将您的应用程序加载到该进程中的同一进程空间中,从而为您提供调试功能,我认为您应该对此进行一些研究。
猜你喜欢
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多