Barry! Thanks for replying! I wish I could have known about this sooner.
Excellent. It seemed I looked forever before I finally stumbled accross the
solution I had origninally.
Here is my confession though... I have 3 seperate browsers in my program.
Contained within ResetAllDataFields(), I destroy and then recreate all of
them (along with resetting a lot of different data). I simply latched onto
myWebBrowser because it was contained withing ResetAllDataFields() and it was
originally the one throwing a COM error when I was calling this from a worker
thread.
It seemed that in my function, if I just checked this object beforehand, I
would know if ResetAllDataFields needed to be invoked on the main thread..
But technically since I am grabbing all 3 browsers and destroying/creating
them within this function, I could have used any of them right there.
Is this sloppy? If so, how can I tell within a function like
ResetAllDataFieldsSafely() whether or not it is being run from a worker
thread or the main thread (without using a random control)?
I put in your suggested code where my original function call was and it
yielded the following error when it came accross another browser object when
destroying and recreating it:
"Controls created on one thread cannot be parented to a control on a
different thread."
(better than the infinite recursion!!)... but this leads me to believe that
even the invoker is trying to launch ResetAllDataFields on the worker
thread....
Am I missing something here?
Thank you so much for your help.
Rob K
"Barry Kelly" wrote:
RobKinney1 <Ro********@discussions.microsoft.comwrote:
private void ResetAllDataFieldsSafely()
{
if(myWebBrowser.InvokeRequired) //always evaluates to true
{
SafelyResetAllDataFieldsDD d = new
SafelyResetAllDataFieldsDD(ResetAllDataFieldsSafel y);
this.Invoke(d);
}
else
{ // now this should be running on the main GUI thread
ResetAllDataFields();
}
} // end ResetAllDataFieldsSafely ()
myWebBrowser.InvokeRequired always is true now!
Interesting. I must say, in C# 2.0, I would write the following:
myWebBrowser.Invoke((MethodInvoker) delegate
{
ResetAllDataFields();
});
.... and rely on Invoke dispatching it properly.
-- Barry
--
http://barrkel.blogspot.com/