views:

57

answers:

2

Hello. I try to use this code :

webBrowser.Document.GetElementById("login").SetAttribute("value", "user");

It work great but not when i use it in a new thread. I get an InvalidCastException. What can I do ?

A: 

webBrowser must be a GUI element, and most GUI elements do not handle multi-threading well. You should typically only access GUI objects on the main UI thread of the application.

The easiest way to delegate calls to the UI thread is to use Dispatcher.Invoke.

Jacob
`Dispatcher.Invoke` that seems pretty WPF specific...
leppie
ok thanks but im a beginner, can u give me an example with webbrowser ?
krisox
@leppie: I almost said that, but then I saw Dispatcher is in WindowsBase.dll not PresentationFramework.dll like all the other WPF stuff.
R. Bemrose
We haven't been told what type `webBrowser` is. If it's a `System.Windows.Forms.WebBrowser`, you can just use its `Invoke` or better yet `BeginInvoke` method.
Jacob
A: 

any other ideas ?

krisox