Parameter objects corrupt after Delegat.BeginInvoke

  • Thread starter Thread starter Marc Bartsch
  • Start date Start date
M

Marc Bartsch

Hi,

I use a delegate to call a method from my main GUI thread. This delegate
takes one of my business objects as a parameter. When I do a synchronous
call, it all works fine, but if I do an async call like:

delegate.BeginInvoke( myBusinessObject, callBackDelegate, delegate );

and I debug into the called method, I find that myBusinessObject is
partially corrupt. All basic type members, like strings etc. are fine, but
my reference members to other objects from the original thread are
non-existent anymore and the debugger cannot resolve them. When I check the
object on the caller's side, i.e. right after the BeginInvoke call, it is
fine, too.

I checked the MSDN, but they always use plain old datatypes as examples.
Maybe it only works for them?

If anyone could enlighten me, why my objects are corrupt, I would be very,
very grateful.

Thanks,

Marc.
 
Marc,

I don't think they become corrupt because of anything that the CLR is
doing. It ^could^ be an issue with the debugger, and the fact that you are
jumping threads. However, the possiblity exists that something else is
making calls to the object on more than one thread at a time, causing the
corruption.

Are you sure you aren't modifying the object after you pass the
reference to the delegate to be invoked asynchronously?
 
Hi,

Thanks for your answer.

It just turned out that a wrong mutex.WaitOne() was responsible for it.
Whenever I stayed within the same thread, the wrong mutex.WaitOne() would
not matter, but in a different thread, it would freeze that thread. Since
the thread was frozen, the debugger could not resolve all my members. ;-)

Thanks anyway,

Marc.

Nicholas Paldino said:
Marc,

I don't think they become corrupt because of anything that the CLR is
doing. It ^could^ be an issue with the debugger, and the fact that you
are jumping threads. However, the possiblity exists that something else
is making calls to the object on more than one thread at a time, causing
the corruption.

Are you sure you aren't modifying the object after you pass the
reference to the delegate to be invoked asynchronously?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Bartsch said:
Hi,

I use a delegate to call a method from my main GUI thread. This delegate
takes one of my business objects as a parameter. When I do a synchronous
call, it all works fine, but if I do an async call like:

delegate.BeginInvoke( myBusinessObject, callBackDelegate, delegate );

and I debug into the called method, I find that myBusinessObject is
partially corrupt. All basic type members, like strings etc. are fine,
but my reference members to other objects from the original thread are
non-existent anymore and the debugger cannot resolve them. When I check
the object on the caller's side, i.e. right after the BeginInvoke call,
it is fine, too.

I checked the MSDN, but they always use plain old datatypes as examples.
Maybe it only works for them?

If anyone could enlighten me, why my objects are corrupt, I would be
very, very grateful.

Thanks,

Marc.
 
Back
Top