Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Asynchronous call without EndInvoke?

Take the following classes as an example.

public class A
{
   // ...
   void Foo(S myStruct){...}
}

public class B
{
   public A test;
   // ...
   void Bar()
   {
      S myStruct = new S();
      test.Foo(myStruct);
   }
}

Now, I want the method-call test.Foo(myStruct) to be an asynchronous call ('fire-and-forget'). The bar-method needs to return as soon as possible. Documentation around delegates, BeginInvoke, EndInvoke, the ThreadPool etc. isn't helping me find a solution.

Is this a valid solution?

     // Is using the `EndInvoke` method as the callback delegate valid?
     foo.BeginInvoke(myStruct, foo.EndInvoke, null);
like image 847
Juergen Avatar asked Sep 20 '11 22:09

Juergen


3 Answers

I would say that your best option is to use the ThreadPool:

void bar()
{
    ThreadPool.QueueUserWorkItem(o=>
    {
        S myStruct = new S();
        test.foo(myStruct);
    });
}

This will queue the snippet for execution in a separate thread. Now you also have to be careful about something else: if you have multiple threads accessing the same instance of A and that instance modifies a variable, then you must ensure that you do proper synchronization of the variable.

public class A
{
    private double sum;
    private volatile bool running;
    private readonly object sync;
    public A()
    {
        sum = 0.0;
        running = true;
        sync = new object();
    }

    public void foo(S myStruct)
    {
        // You need to synchronize the whole block because you can get a race
        // condition (i.e. running can be set to false after you've checked
        // the flag and then you would be adding the sum when you're not 
        // supposed to be).
        lock(sync)
        {
            if(running)
            {
                sum+=myStruct.Value;
            }
        }
    }

    public void stop()
    {
        // you don't need to synchronize here since the flag is volatile
        running = false;
    }
}
like image 200
Kiril Avatar answered Oct 08 '22 17:10

Kiril


You are not required to call EndInvoke; not calling it merely means:

  • You don't get the return value from the method.
  • Any exceptions thrown during the method execution will simply disappear.

It sounds like you want to 'fire-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)
   {
      // Log the message?
   }
}, null);

This is what happens when you execute this code:

  1. A new thread is allocated (put simply) for the delegate.
  2. The thread is given the delegate del and the anonymous delegate (iar => ...).
  3. The thread executes del.
  4. When it is finished executing (or an exception occurs) the result or exception is stored and the anonymous delegate is executed.
  5. Inside the anonymous delegate, when EndInvoke is called the result from the method is either returned, or the exception is thrown (if one occurred).

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've never found a scenario where not calling it presents a problem, however that is an implementation detail and is relying on undocumented behavior.

Finally your solution would crash the process if an exception is thrown, you can simply pass null as the delegate if you don't care about the exception (del.BeginInvoke(myStruct, null, null);). So as a final example what you are looking for is probably:

public class A
{
    // ...
    void Foo(S myStruct){...}
    void FooAsync(S myStruct)
    {
        var del = new Action<S>(Foo);
        del.BeginInvoke(myStruct, SuppressException, del);
    }

    static void SuppressException(IAsyncResult ar)
    {
        try
        {
            ((Action<S>)ar.AsyncState).EndInvoke(ar);
        }
        catch
        {
            // TODO: Log
        }
    }
}
like image 44
Jonathan Dickinson Avatar answered Oct 08 '22 16:10

Jonathan Dickinson


You can use the Callback model explained @ What is AsyncCallback?

That way your EndInvoke will not be in bar(), but in a separate callback method.

In the example, the EndRead (corresponding to EndInvoke is in the callback method called CompleteRead rather than the calling method TestCallbackAPM corresponding to bar)

like image 30
SO User Avatar answered Oct 08 '22 17:10

SO User