.NET Webbrowser Control and Dispose()

后端 未结 3 1365
生来不讨喜
生来不讨喜 2021-01-14 01:03

I know this is a hot discussed topic with many questions and answers but I still do not find the solution for the following problem:

I have a multi-tab application.

相关标签:
3条回答
  • 2021-01-14 01:45

    All WebBrowser instances share the session on per-process basis. According to EricLaw's answer to a similar question, it appears to be impossible to separate sessions. I'd trust Eric's statement as he worked as IE program manager at Microsoft.

    If however you'd still like to try some hacks, you may look at CoInternetGetSession. First, try saving and holding on to the returned reference to IInternetSession. Further, you could look at registering your own URL namespace (RegisterNameSpace) and implementing a pluggable protocol handler which may eventually allow to overrule this restriction.

    Of course, it sounds like an overkill and most likely won't help at all. A clean solution might be to redesign the logic to get rid of cookies and pass the state via URLs.

    EDITED: Another idea, try to navigate the WebBrowser instance to (say) "about:blank" and wait for DocumentComplete event, before actually disposing of it with Dispose().

    0 讨论(0)
  • 2021-01-14 01:58

    This seems to be a GarbageCollector-Problem. You can try to use the dirty way of System.GC.Collect(), just calling the GarbageCollector to free memory, but this is not a good way to solve the problem.

    Of what you told, this seems to be a Pointer-problem. If you declared the Connection as a global variable, you have to detach the connection from the tab befor you can close/dispose the tab itselve. The event Me.Closing shuld help you to do so. If the Pointer stays open, the tab as an object is still connected on the Connection and will (not realy shure if/when) not get cleaned by the GC.

    If you can clarify your way of duplicating/referencing the connection, I could give a more detailed answer.

    EDIT: after a while of research my worries became true - there is a problem with caching under IE (>5 as far as I know). http://social.msdn.microsoft.com/Forums/ie/en-US/88c21427-e765-46e8-833d-6021ef79e0c8/memory-leak-in-ie-webbrowser-control

    Suggestions are:

    • calling GarbageCollector manually

    • limit MemUsage (can result in application-crashes and also just writes the pages to disk)

    • about:blanc to override cache-entries

    • calling C++ methods to override the cache (WinINet - all I have found resulted in some ProtectedMemory-Errors - maybe this C# WebBrowser control: Clearing cache without clearing cookies works)

    • using C++ and WinINet (I don't know any real .Net implementation and it may also have this memory leak)

    • using alternatives to IE like gecko (Mozilla) - https://bitbucket.org/geckofx/

    0 讨论(0)
  • 2021-01-14 02:06

    Thanks for your answers. Here what I checked out:

    Calling GC manually:

    • Does only help if I use Webbrowser.Dispose() in before. But this is not a solution because of the session problem.

    Limit Mem Usage:

    • Not a solution. This program should run a whole day with many opening and closing of tabs. If I can not clean the used memory, the memory usage will be too much after some hours..

    about:blank:

    • I called about:blank on Closing the Tab. After DocumentLoaded occurs for this URL I disposed the Webbrowser. Same procedure as calling Dispose directly. Session breaks down.

    Other components:

    • I need to have an IE control in every case because the (proprietary) internet application only supports IE 8 and higher.

    "Using c++ and WinInet":

    • Can I use the C++ Browser in my .net program? I can not switch the whole program to C++. This wouldn't be a solution for me.

    In summary:

    My application works fine without Dispose but has the problem with the increasing memory usage. If we could find a solution for this (which seems to be impossible) it would be the best solution.

    The only thing what would be an acceptable "workaround" for me is to reuse the "closed" webbrowsers. In detail: On every tab close I add the Webbrowser to a List instead of Dispose them. When I need a new tab I take the first out of the list an reuse it and Navigate to the new URL: I tried it out but it seems there is the same problem with the sessions. The sessions in the reused tabs seems to be new again. But I really do not understand why... An suggestions for this, too?

    Another workaround would be to force every Webbrowser object to be a single instance. Is this possible?

    0 讨论(0)
提交回复
热议问题