开发者

Implementing Communication Between JavaScript Code and Console Application C#

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开发者_Go百科;

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜