Help Finding Cross-Thread Error

T

TC

I'm getting a "Cross-thread operation not valid" error. Unfortunately,
when Visual Studio reports the error, it doesn't show me the line of
code where the error occurs. Also, it says the name of the offending
control is '' (empty string). As a result, I have no idea what part of
the code is causing the error.

I've gone through the code section by section and I've named every
control I can find. I've also written code to automatically name
controls by iterating recursively through the Controls collection of
my form. Nevertheless, Visual Studio still reports a cross-thread
error with control ''.

I'm aware of the rules for multi-threading, so if I find the error I
can fix it. Can anyone give me advice on how to find it?


-TC
 
A

Armin Zingler

TC said:
I'm getting a "Cross-thread operation not valid" error. Unfortunately,
when Visual Studio reports the error, it doesn't show me the line of
code where the error occurs. Also, it says the name of the offending
control is '' (empty string). As a result, I have no idea what part of
the code is causing the error.

I've gone through the code section by section and I've named every
control I can find. I've also written code to automatically name
controls by iterating recursively through the Controls collection of
my form. Nevertheless, Visual Studio still reports a cross-thread
error with control ''.

I'm aware of the rules for multi-threading, so if I find the error I
can fix it. Can anyone give me advice on how to find it?


When the error occurs, what does the callstack window show?
 
T

TC

When the error occurs, what does the callstack window show?

Armin,

Thank you for the reply. Your question nudged me closer to a solution.
The Call Stack window says "[External Code]", which doesn't help, but
the stack trace in the error message says this:

at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_InternalHandle()
at System.Windows.Forms.Control.DestroyHandle()
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at Dundas.Charting.WinControl.Selection.a()
at Dundas.Charting.WinControl.Chart.Dispose(Boolean disposing)
at System.ComponentModel.Component.Finalize()
at Dundas.Charting.WinControl.Chart.Finalize()

If I read that correctly, I should be looking for a place where I'm
releasing a Dundas.Charting.WinControl.Chart object. That narrows
things down a bit.

-TC
 
A

Armin Zingler

TC said:
When the error occurs, what does the callstack window show?

Armin,

Thank you for the reply. Your question nudged me closer to a solution.
The Call Stack window says "[External Code]",

In the call stack's context window, you can enable the display of that
External Code.
which doesn't help, but
the stack trace in the error message says this:

at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_InternalHandle()
at System.Windows.Forms.Control.DestroyHandle()
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at Dundas.Charting.WinControl.Selection.a()
at Dundas.Charting.WinControl.Chart.Dispose(Boolean disposing)
at System.ComponentModel.Component.Finalize()
at Dundas.Charting.WinControl.Chart.Finalize()

If I read that correctly, I should be looking for a place where I'm
releasing a Dundas.Charting.WinControl.Chart object. That narrows
things down a bit.

This is obviously a GC thread. It's about to destroy some objects. Hard
for me to say from here how it comes to this problem, but you're on the
right track.

..... But wait, I wonder how it can happen that a Control gets destroyed
without having been disposed _before_. Because: if the Window hasn't
been closed yet, it won't be destroyed, and if it already has been
closed, it has also been disposed (unless shown modally, but that's
another story).

....Some hours later: ;)
Inside Finalize, Dispose is called with argument disposing=false. Then,
I don't know what you're doing insided Selection.a(). It seems that
MyBase.Dispose() is called, but this should only be done if
disposing=True. At least that's what's usually done in the designer
generated code. However, Selection.a() does not have this argument.
Therefore it's interesting what happens inside Selection.a().
 
A

Alex Clark

Hi TC,

First the obvious question - are you doing something in a background worker
thread?

When the code breaks, what threads do you have listed in the toolbar (right
click toolbar, ensure "Debug Location" is ticked)?

If you're not doing any threading work yourself, it's possible you've found
a bug in the Dundas charting controls (which themselves may be doing some
sort of background thread work).

HTH,
Alex
 

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