2013-05-16 62 views
2

我目前正在使用WinForms主機中WinForms WebBrowser控件的應用程序。問題在於,即使關閉瀏覽器並處理內存似乎沒有被釋放的所有內容。WPF內存泄漏的WinForms WebBrowser

我發現這個職位,它看起來像udione的回答可能是什麼我要找的。問題是我不知道如何讓它通過WPF的WinForms控件工作。

How to get around the memory leak in the .NET Webbrowser control?

這是我改變了它太多,但它似乎並沒有這樣的伎倆。我不知道有關this.webbrowser.webBrowser

public void DisposeWebBrowser(System.Windows.Forms.WebBrowser browser) 
    { 
     //dispose to clear most of the references 
     browser.Dispose(); 

     //REMOVED THIS LINE 
     //BindingOperations.ClearAllBindings(browser.); 

     //using reflection to remove one reference that was not removed with the dispose 
     var field = typeof(System.Windows.Window).GetField("_swh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 

     //CHANGED THIS LINE 
     //var valueSwh = field.GetValue(mainwindow); <-- OLD LINE 
     var valueSwh = field.GetValue(GetWindow(this)); 

     var valueSourceWindow = valueSwh.GetType().GetField("_sourceWindow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSwh); 

     var valuekeyboardInput = valueSourceWindow.GetType().GetField("_keyboardInputSinkChildren", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSourceWindow); 

     System.Collections.IList ilist = valuekeyboardInput as System.Collections.IList; 

     lock (ilist) 
     { 
      for (int i = ilist.Count - 1; i >= 0; i--) 
      { 
       var entry = ilist[i]; 
       var sinkObject = entry.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 

       //CHANGED THIS LINE 
       //if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser)) <-- OLD LINE 
       if (object.ReferenceEquals(sinkObject.GetValue(entry), browser)) 
       { 
        ilist.Remove(entry); 
       } 
      } 
     } 
    } 

我如何能得到這個工作任何想法或更好的方式來處理這個問題?

回答

2

是的。任何.NET擁有並涉及IE瀏覽器的WebBrowser都是純粹的邪惡,應該永遠不要使用,因爲它的行爲因計算機而異。使用CefSharp(首選)或Awesomium。