InvokeRequired and CreateGraphics

F

Fabien Bezagu

Hi,

Here is what the documentation of InvokeRequired says :

"In addition to the InvokeRequired property, there are four methods on a
control that are safe to call from any thread: Invoke, BeginInvoke,
EndInvoke and CreateGraphics. For all other method calls, you should use one
of these invoke methods when calling from a different thread. "

Could someone explain me why a call to CreateGraphics is "safe" ? One of my
backgroud threads calls CreateGraphics on a control but when this control is
destroyed (ie the application is closed), this thread is blocked on the call
to CreateGraphics and isn't aborted. I don't understand why a call to Invoke
isn't required in this case.

Thanks

Fabien
 
W

Willy Denoyette [MVP]

Fabien Bezagu said:
Hi,

Here is what the documentation of InvokeRequired says :

"In addition to the InvokeRequired property, there are four methods on a
control that are safe to call from any thread: Invoke, BeginInvoke,
EndInvoke and CreateGraphics. For all other method calls, you should use
one of these invoke methods when calling from a different thread. "

Could someone explain me why a call to CreateGraphics is "safe" ? One of
my backgroud threads calls CreateGraphics on a control but when this
control is destroyed (ie the application is closed), this thread is
blocked on the call to CreateGraphics and isn't aborted. I don't
understand why a call to Invoke isn't required in this case.

Thanks

Fabien

Because, just like the other methods, CreateGraphics marshals the call to
the owning thread ot the Control.
Are you sure your thread is marked as Background, and that you disposed the
Graphics object returned from CreateGraphics ()?

Willy.
 
F

Fabien Bezagu

If CreateGraphics marshals the call to the owning thread (because Graphics
inherits from MarshalByRefObject I suppose), what happens if the owning
thread is terminated when the background thread (I'm sure it's marked
background) is trying to call CreateGraphics ?

When I close the main window in debug mode, the background is sometimes not
terminated. I can then break all and see that the current thread (the
background one) is on the line :

using (Graphics g = container.CreateGraphics())

where container is my (custom) control created by the main thread I just
closed....

Here is the Call stack at this precise time :
--------------------------------------
[In a sleep, wait, or join]
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() +
0x1e7 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get() + 0x7c
bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateGraphicsInternal()
+ 0x5 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateGraphics() +
0x53 bytes
TestGraphics.EXE!TestGraphics.Voie.derivationCourante_NouvelEchantillon(object
sender = {TestGraphics.Derivation}, System.EventArgs e =
{System.EventArgs}) Line 311 + 0xd bytes C#
TestGraphics.EXE!TestGraphics.Derivation.OnNouvelEchantillon(System.EventArgs
e = {System.EventArgs}) Line 61 + 0x10 bytes C#
TestGraphics.EXE!TestGraphics.Derivation.Add(byte delta = 224) Line 95 +
0xd bytes C#
TestGraphics.EXE!TestGraphics.Form4.CreerSignal() Line 89 + 0x21 bytes C#
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
state) + 0x3b bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
executionContext, System.Threading.ContextCallback callback, object state) +
0x7d bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x40 bytes
--------------------------------------

I'm a bit lost....

Fabien

PS : I use a beta version of the framework 2.0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top