2010-02-09 40 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中使用它可能更容易。

相關問題