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

编程入门 行业动态 更新时间:2024-10-25 21:16:11
本文介绍了是否可以从另一个 c# 控制台应用程序控制 WinForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 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");
                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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 21:24:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   程序控制   WinForm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!