2010-02-09 38 views
1

现在我正在解决Web服务器性能测试任务并出现一些小问题。在JavaScript代码和控制台应用程序之间实现通信C#

有一种方法来communication between DHTML and C# code。它在Win应用程序中完美工作,但我需要Console应用程序。 所以我创建简单的测试

的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( '测试'); ? ,但没有成功:-(

任何建议

回答

0

我觉得你很可能有相同的proplem的家伙在这里:WebBrowser Control - Console Application - Events Not Firing

也许他得到的回答可以帮助你,太消息。从一个控制台应用程序不会被触发,因为:

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

相关问题