Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are not required to call <code>EndInvoke</code>; not calling it merely means:</p> <ul> <li>You don't get the return value from the method.</li> <li>Any exceptions thrown during the method execution will simply disappear.</li> </ul> <p>It sounds like you want to 'fire-and-forget', so the easiest way to do this is to use an anonymous delegate, for example:</p> <pre><code>var del = new Action(foo.Bar); del.BeginInvoke(iar =&gt; { try { del.EndInvoke(iar); } catch (Exception ex) { // Log the message? } }, null); </code></pre> <p>This is what happens when you execute this code:</p> <ol> <li>A new thread is allocated (put simply) for the delegate.</li> <li>The thread is given the delegate <code>del</code> and the anonymous delegate (<code>iar =&gt; ...</code>).</li> <li>The thread executes <code>del</code>.</li> <li>When it is finished executing (or an exception occurs) the result or exception is stored and the anonymous delegate is executed.</li> <li>Inside the anonymous delegate, when <code>EndInvoke</code> is called the result from the method is either returned, or the exception is thrown (if one occurred).</li> </ol> <p>Note that the above example is very different from:</p> <pre><code>// This is pointless and is still, essentially, synchronous. del.EndInvoke(del.BeginInvoke(null, null)); </code></pre> <p><strong>Edit:</strong> You should always call <code>End*</code>. I've never found a scenario where not calling it presents a problem, however that is an implementation detail and is <a href="https://stackoverflow.com/questions/4585042/is-delegate-endinvoke-really-necessary">relying on undocumented behavior.</a></p> <p>Finally your solution would crash the process if an exception is thrown, <strike>you can simply pass null as the delegate if you don't care about the exception (<code>del.BeginInvoke(myStruct, null, null);</code>).</strike> So as a final example what you are looking for is probably:</p> <pre><code>public class A { // ... void Foo(S myStruct){...} void FooAsync(S myStruct) { var del = new Action&lt;S&gt;(Foo); del.BeginInvoke(myStruct, SuppressException, del); } static void SuppressException(IAsyncResult ar) { try { ((Action&lt;S&gt;)ar.AsyncState).EndInvoke(ar); } catch { // TODO: Log } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload