You do not need to call EndInvoke ; without naming it simply means:
- You will not get the return value from the method.
- Any exceptions that occur during the execution of the method will simply disappear.
It sounds like you want to "burn out and forget", so the easiest way to do this is to use an anonymous delegate, for example:
var del = new Action(foo.Bar); del.BeginInvoke(iar => { try { del.EndInvoke(iar); } catch (Exception ex) {
Here's what happens when this code is executed:
- A new thread is allocated (simply) to the delegate.
- The stream specifies the
del delegate and anonymous delegate ( iar => ... ). - The thread does
del . - When a completion is performed (or an exception occurs), the result or exception is saved and an anonymous delegate is executed.
- Inside an anonymous delegate, when
EndInvoke is called, the result from the method is either returned or an exception is thrown (if it happened).
Please note that the above example is very different from:
// This is pointless and is still, essentially, synchronous. del.EndInvoke(del.BeginInvoke(null, null));
Edit: You should always call End* . I have never met a script when it was not caused by a problem, but this is a detailed implementation and relies on undocumented behavior.
Finally, your solution will crash the process if an exception is thrown, you can just pass null as a delegate if you don't care about the exception ( del.BeginInvoke(myStruct, null, null); ). So, as the final example you are looking for is probably:
public class A {
Jonathan Dickinson Sep 22 '11 at 8:16 2011-09-22 08:16
source share