0

内で実行されているクラス内でWebブラウザーを実行して、それが最初のWindowsで実行していたことはフォームアプリケーション、今私はそれを変換しますそれを行うには、コンソールアプリケーションである私はスレッドで私のクラスを実行し、STAで設定し、私のクラスの中で私は "新しい"キーワードを使用して通常の方法でWebブラウザーをインスタンス化する、私の問題は、私はThread.Join()を使用してEndless waitingを入力し、Application.ThreadExit()を使用しても何も起こらないので、Thread.Abort()を試したので、例外が無作為にスローされ続けます。コンソールアプリケーションでWebBrowserをインスタンス化する必要があるクラスを実行します。コンソールアプリケーション:私はいくつかのテキストデータを掻き出すために、WebBrowserコントロールを使用するクラスを作成したSTAスレッド

これは私が私のクラスのためのスレッドを作成する方法である:

var th = new Thread(() => 
{ 
    var br = MyBrowsingClass; 
    br.NavigateTo(url); 
    System.Windows.Forms.Application.Run(); 
}); 
th.SetApartmentState(ApartmentState.STA); 
th.Start(); 
return th; 

と私のクラスの中、私は次のようにします。

wb = new WebBrowser(); 
wb.ScriptErrorsSuppressed = true; 
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnWebComplete); 
CurrentState = Status.Navigating; 
if (IsValidURL(GoogleQueryURL)) 
{ 
    wb.NavigateTo(GoogleQueryURL); 
} 
+0

MSDNから問題が一つだけのインスタンスが存在し得るということです。したがって、並列コンテンツ取得は簡単に行えません。 'Application.Exit()'を呼び出すことで 'Application.Run()'を終了するか、 'ダミー 'フォームを作成して' Application.Run(form1) 'に渡して' form1.Close() 'を閉じることができます。しかし、以下で説明したように、 'WebRequest'クラスを見てください。 –

答えて

0

私はあなたがしたいと思うので、あなたは、WebRequestクラスを使用する必要がありますコンテンツをインタラクティブブラウザではなく、これにより、余分なスレッドでメッセージポンプ(Application.Run())を使用する必要がなくなります。 `Application.Run()`と

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace Examples.System.Net 
{ 
    public class WebRequestGetExample 
    { 
     public static void Main() 
     { 
      // Create a request for the URL.   
      WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html"); 
      // If required by the server, set the credentials. 
      request.Credentials = CredentialCache.DefaultCredentials; 
      // Get the response. 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      // Display the status. 
      Console.WriteLine (response.StatusDescription); 
      // Get the stream containing content returned by the server. 
      Stream dataStream = response.GetResponseStream(); 
      // Open the stream using a StreamReader for easy access. 
      StreamReader reader = new StreamReader (dataStream); 
      // Read the content. 
      string responseFromServer = reader.ReadToEnd(); 
      // Display the content. 
      Console.WriteLine (responseFromServer); 
      // Cleanup the streams and the response. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 
     } 
    } 
} 

SOURCEhttps://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

+1

webclientの問題は、生のhtmlをスクリプトで満たしていることです。 Webブラウザはすべての問題を解決して、より良いhtml結果を提供します...私はwebbrowser control.iに固執したいと思います。ダミーフォームを使用して試してみてください....すべてに感謝し、スレッドを更新します問題 –

関連する問題