Help Finding Cross-Thread Error

  • Thread starter Thread starter TC
  • Start date Start date
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
 
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?
 
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
 
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().
 
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
 
Back
Top