What if I don't use Control.Invoke?

  • Thread starter Thread starter JO
  • Start date Start date
J

JO

In the docs, they state that if I were to want to make changes to a control
created on another thread, I should use Control.Invoke. What if I don't?
I've accidentally tried this, and it seems to work, MOST of the time.
However, sometimes my form would spuriously close - is that possible, or is
that just a separate bug?

Regardless, I changed my calls to Control.Invoke when I realized this, but
I'm trying to see if that was the source of my form closing sometimes. Does
anyone know what happens under the hood?
 
It's possible that the behavior you have seen is related to trying to update
a control from another thread. You should always use Control.Invoke in these
types of cases. Usually, from what I've seen, the application will just
become unresponsive (hang). But other erratic behavior wouldn't surprise me.
As a hard rule you should always call Control.Invoke when updating a control
from any other thread than the one that "owns" the control.
 
JO said:
In the docs, they state that if I were to want to make changes to a control
created on another thread, I should use Control.Invoke. What if I don't?
I've accidentally tried this, and it seems to work, MOST of the time.

Yup, that's what you should expect.
However, sometimes my form would spuriously close - is that possible, or is
that just a separate bug?

That sounds possible. Basically, the behaviour isn't well-defined if
you don't follow the rules, as far as I know.
Regardless, I changed my calls to Control.Invoke when I realized this, but
I'm trying to see if that was the source of my form closing sometimes. Does
anyone know what happens under the hood?

That might be the source, yes.

I believe in Longhorn, Avalon code will actually through an exception
if you use the wrong thread. At least, that's what I've heard talked
about before.
 
Back
Top