实现 JavaScript 代码与控制台应用程序 C# 之间的通信

发布于 2024-08-20 13:50:12 字数 1399 浏览 3 评论 0原文

现在我正在解决网络服务器性能测试任务,但遇到了一个小问题。

有一种方法可以DHTML 和 C# 代码之间进行通信。它在 Win 应用程序中完美运行,但我需要控制台应用程序。 所以我创建了简单的测试

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CometTest
{    
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Client c = new Client(@"URL");
            Console.ReadLine();
            Console.WriteLine(c.ToString());

        }

    }
}
    

Client.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;

namespace CometTest
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class Client
    {
        public WebBrowser browser;

        public Client(string host)
        {
            browser = new WebBrowser();
            browser.ObjectForScripting = this;
            browser.Navigate(host);      
        }
        public void Notify(String message)
        {
            Console.WriteLine(message);
        }
    }
}
    

在我的 JavaScript 代码中执行 window.external.Notify('测试'); ,但没有成功:-(

有什么建议吗?

Now I'm solving web server performance testing task and a have a little problem.

There is a way to communication between DHTML and C# code. It works perfectly in Win Application but I need Console Application.
So I create simple test

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CometTest
{    
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Client c = new Client(@"URL");
            Console.ReadLine();
            Console.WriteLine(c.ToString());

        }

    }
}
    

Client.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;

namespace CometTest
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class Client
    {
        public WebBrowser browser;

        public Client(string host)
        {
            browser = new WebBrowser();
            browser.ObjectForScripting = this;
            browser.Navigate(host);      
        }
        public void Notify(String message)
        {
            Console.WriteLine(message);
        }
    }
}
    

In my JavaScript code I perform
window.external.Notify('test');
, but without success :-(

Any Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-08-27 13:50:12

我认为你几乎和这里的人有同样的问题:WebBrowser Control - 控制台应用程序 - 事件未触发

也许他得到的答案也对您有帮助。来自控制台应用程序的消息不会被触发,因为:

STA线程的基本要求是它需要运行消息泵。在 Windows 窗体中,您可以使用 Application.Run。或者您可以使用 user32!GetMessage & 手动编写消息泵。调度消息。但在 WinForms 或 WPF 中使用可能更容易。

I think you pretty much have the same proplem as the guy here: WebBrowser Control - Console Application - Events Not Firing

Maybe the answer he got helps you, too. The messages from an console application are not fired because:

The basic requirement of an STA thread is that it needs to run a message pump. In Windows Forms, you can use Application.Run. Or you could write the message pump by hand, using user32!GetMessage & DispatchMessage. But it's probably easier to use the one in WinForms or WPF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文