繁体   English   中英

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

[英]Is it possible to have control on WinForm from another c# console app?

我只有2天大的.NET C#编码器,试图从ProjectB-C#控制台应用程序获得对ProjectA-WinForm的控制权

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

当我运行执行winformWithWebBrowserTest.exe的ProjectB时,显示带有webbrowser的winform。 但是随后它无法访问form1。 如何从ProjectB访问Web浏览器控件?

错误:

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天的进程:)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM