1

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.

snowell
  • 89
  • 2
  • 10
  • As i think (it's just a suggestion), this parameter is a parameter, which will be passed to callback on it's ivocation – Alex Voskresenskiy Feb 12 '15 at 09:25
  • 1
    "I did find it in MSDN" - no you didn't; you found two other unrelated methods with the same name. Just because it has the same name doesn't mean it has the same meaning; `Delegate.BeginInvoke` is very different to `Control.BeginInvoke`, for example. What does the intellisense or documentation say about DataVirtualization's `BeginInvoke`? Heck, *what is the parameter called*? – Marc Gravell Feb 12 '15 at 09:25
  • 1
    A somewhat similar question: http://stackoverflow.com/questions/14961450/where-are-clr-defined-methods-like-delegate-begininvoke-documented – BCdotWEB Feb 12 '15 at 09:26

1 Answers1

1

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).
Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
  • `TId` is passed to `AsyncCallback`? But `AsyncCallback` only has one parameter which is `IAsyncResult` type. And when the code use it, pass an int to it. So how could it be the parameter passed to the delegate method? – snowell Feb 12 '15 at 09:55
  • No, the `TId` is not passed to he AsyncCallback. It's passed to `Load`. `Load` is a `Func`, so it expects a `TId` as first parameter. You call `Load(somethingThatIsTid)` synchronously and `Load.BeginInvoke(somethingThatIsTid, somethingThatIsCalledWhenLoadFinished, something)` asynchronously. – sloth Feb 12 '15 at 10:08
  • The third parameter, the object, according to the document, "This object can be used to pass application-specific state information to the method invoked when the asynchronous operation completes." I wonder, how does it pass to the delegate method. By passing parameters or what? – snowell Feb 14 '15 at 04:59