Re: Control.BeginInvoke is NOT fire-and-forget

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Peter Duniho wrote:
On Tue, 08 Jul 2008 08:25:37 -0700, Ben Voigt [C++ MVP]
<rbv@xxxxxxxxxxxxx> wrote:

As much as the CLR team assures us that it's ok to fire-and-forget
Control.BeginInvoke, it seems it isn't. Maybe this is a bug.

See for example: the comments in
http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx

[...]
Do you think this is a CLR bug or it is by design?

First thing to keep in mind: the assertion about Control.EndInvoke()
has to do with resource cleanup and whether one is required to call
that method to ensure things are cleaned up. It's not about whether
Control.BeginInvoke() will work.

Second, it would be helpful if you'd actually post a
concise-but-complete code sample that reliably demonstrates the
problem. Saying "I original had c.BeginInvoke in the asynchronous
method" doesn't tell us much about how you actually used it or what
might have been going wrong.

Did I write that? Sure enough.

I should have said "in the anonymous method".

i.e. changing the line from
helper.postProcessing = delegate { c.Invoke(finalProcessing); };
back to
helper.postProcessing = delegate { c.BeginInvoke(finalProcessing); };

breaks things, in that the finalProcessing MulticastDelegate never runs nor
throws an exception.


I am relatively confident that if you call BeginInvoke() from a thread
that exits before the invoked delegate gets to run, the invoked
delegate should still run. I would be very surprised if that wasn't
actually what happened. On the other hand, if the thread that _owns_
the control being used to call BeginInvoke() exits or is otherwise
terminated, I would _not_ expect the delegate being invoked to
execute, since it has to execute on that thread.

The control should be owned by the original thread which does not exit, I
read the Handle property for the explicit purpose of forcing it to be
created on that thread, before I spawn the worker.


Again, a complete code sample would eliminate these ambiguities in
your comment. It's impossible to tell for sure from the code you
posted what exactly you were trying to do and what broke. It also
doesn't help that the code you posted is clearly a corner case,
whatever else might have been going on, and you didn't post enough to
show us that you've correctly set the threading model for whatever
threads wind up with a message pump (something that could also break
things).

Changing the call in the asynchronous method which is called at the very end
of the worker threadproc from BeginInvoke to Invoke does cure the problem.
I think this demonstrates that the thread on which the control is created is
properly pumping messages.


Given the evidence so far, I cannot imaging being able to confidently
say there's a bug, whether in the CLR or (as is probably more likely,
assuming this is a bug at all) in the framework.

True, it's most likely a bug in the base class libraries, not the CLR. My
poor wording.


Pete


.



Relevant Pages

  • RE: MissingMethodException with MulticastDelegate
    ... You are hitting a bug in .NETCF v1 on ARM-compatible devices. ... delegates and invoke each one separately. ... This bug does not exist in any of the pre-release versions of .NETCF v2. ... >delegate) I can catch the exception. ...
    (microsoft.public.dotnet.framework.compactframework)
  • [0xbadc0ded #03] DeleGate (SSL-filter) <= 8.9.2
    ... DeleGate is a multi-purpose application level gateway which runs on ... The bug can be triggered using a certificate with field contents ... randomization may be considered good enough by some. ... - Custom System Development for Unix/Linux/BSD and Windows ...
    (Bugtraq)
  • [Full-Disclosure] [0xbadc0ded #03] DeleGate (SSL-filter) <= 8.9.2
    ... DeleGate is a multi-purpose application level gateway which runs on ... The bug can be triggered using a certificate with field contents ... randomization may be considered good enough by some. ... - Custom System Development for Unix/Linux/BSD and Windows ...
    (Full-Disclosure)
  • System.Threading.Timer improper function in Release mode
    ... I've encountered a strange phenomena which appears to me as a bug: ... I have an engine that uses a System.Threading.Timer to invoke a delegate ... The workaround solution I used was declaring the timer as static. ...
    (microsoft.public.dotnet.languages.csharp)
  • DEVTEAM, a possible bug in VS .NET 2003
    ... handler and the delegate addition, and using the Designer mode (from ... The reason why I believe this to be a bug is in the way InitializeComponent ... manually add event handler code inside the class. ...
    (microsoft.public.dotnet.languages.vc)