【问题标题】:Implementing Communication Between JavaScript Code and Console Application C#在 JavaScript 代码和控制台应用程序 C# 之间实现通信
【发布时间】:2011-01-14 17:27:30
【问题描述】:

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

有一种方法可以communication between DHTML and C# code。它在 Win 应用程序中完美运行,但我需要控制台应用程序。 所以我创建了简单的测试

程序.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());

        }

    }
}
    

客户端.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('test'); ,但没有成功:-(

有什么建议吗?

【问题讨论】:

    标签: c# javascript interop automated-tests comet


    【解决方案1】:

    我认为你和这里的人几乎有同样的问题:WebBrowser Control - Console Application - Events Not Firing

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多