snowell
snowell

Reputation: 89

What does the three-parameters version of BeginInvoke do?

I've been reading some code about Data Virtualization on the internet. I met with a function called BeginInvoke. Basically, I know what it is used for. But this function has a version with three parameters which I didn't look up in MSDN.

I did find it in MSDN which has two parameters at most:

Control.BeginInvoke

Also I don't think this is the one I want:

Dispatcher.BeginInvoke

Because the three-parameters version has a DispatcherPriority type for its first parameter which the code I read doesn't pass.

The code I'm reading is:

using System;
using System.Threading;

namespace WPF.DataVirtualization
{
    public class AsyncDataRef<TId, T> : DataRefBase<T> where T : class
    {
        private readonly TId m_Id;
        private int m_Loading;
        private readonly Func<TId, T> Load;
        private volatile T m_Data;

        public AsyncDataRef(TId id, Func<TId, T> load)
        {
            m_Id = id;
            Load = load;
        }

        public override T Data
        {
            get
            {
                if (m_Data != null)
                    return m_Data;
                if (Interlocked.Increment(ref m_Loading) == 1)
                    if (m_Data == null)
                        Load.BeginInvoke(m_Id, AsyncLoadCallback, null);
                    else
                        Interlocked.Decrement(ref m_Loading);
                else
                    Interlocked.Decrement(ref m_Loading);
                return m_Data;
            }
        }


        private void AsyncLoadCallback(IAsyncResult ar)
        {
            m_Data = Load.EndInvoke(ar);
            Interlocked.Decrement(ref m_Loading);
            // when the object is loaded, signal that all the properties have changed
            NotifyAllPropertiesChanged();
        }
    }
}

And when call this function, it uses it like this:

var list = new List<DataRefBase<Person>>(itemCount);
...
list.Add(new AsyncDataRef<int, Person>(i, LoadPerson));

Person is another kind of class. LoadPerson is a delegate function. DataRefBase is a kind of template class.

My question is this line:

Load.BeginInvoke(m_Id, AsyncLoadCallback, null);

What do the three parameters do? (It seems that m_Id is used as an int)What do they stand for? Where can I find the document of this version of BeginInvoke.

Upvotes: 1

Views: 610

Answers (1)

sloth
sloth

Reputation: 101142

Have a look at Asynchronous Programming Model (APM):

Beginning an Asynchronous Operation

A BeginOperationName method takes any parameters declared in the signature of the synchronous version of the method that are passed by value or by reference. Any out parameters are not part of the Begin_OperationName_method signature. The Begin_OperationName_method signature also includes two additional parameters. The first of these defines an AsyncCallback delegate that references a method that is called when the asynchronous operation completes. The caller can specify null (Nothing in Visual Basic) if it does not want a method invoked when the operation completes. The second additional parameter is a user-defined object. This object can be used to pass application-specific state information to the method invoked when the asynchronous operation completes.

Since Load is a Func<TId, T>, it's BeginInvoke method has three parameters with the following types:

  • TId (passed to the method/delegate that is called asynchronously)
  • AsyncCallback (called when the asynchronous operation completes)
  • object (used to pass around a state).

Upvotes: 1

Related Questions