Search code examples
.netbackgroundworker

Alternative for Cross thread Calls in .net


I am working on an application which has 4 panels. The first panel displays static images, the second however loads a webpage. Since I will be navigating sequentially, I want to load the web page in the second panel before I actually visit it. How to implement it? It is not possible using a new thread or background worker since they won't be thread safe. Is there a better way to do it? How to load the webpage without freezing my UI and is it possible to load it before I actually visit it as I said?


Solution

  • You have no options here, the WebBrowser class is fundamentally thread-unsafe and has a hard thread affinity. There are ways to run it on a separate thread, this answer shows you how. But as long as you also need it to render the page content on your UI then you must write code that's asynchronous. Whatever you do must be implemented in the DocumentCompleted event handler. A simple state machine can do wonders.