1

I am trying to write an async operation using C# and .net 4.5/4.6, and am trying to use Func.BeginInvoke()

Here's what MSDN's BeginInvoke page says:

public virtual IAsyncResult BeginInvoke(
    AsyncCallback callback,
    Object object
)

Unfortunatly there's no information on what the second parameter (object) is, and the primary MSDN Async Tutorial Page shows:

// Initiate the asychronous call.
        IAsyncResult result = caller.BeginInvoke(3000, 
            out threadId, null, null);

which isn't even a supported function definition!

Looks like the .net documentation has gotten substantially worse since last time I looked at it.

Help would be appreciated!

dcastro
  • 66,540
  • 21
  • 145
  • 155
JasonS
  • 7,443
  • 5
  • 41
  • 61
  • 3
    You should consider using `TPL` and `async/await` features for asynchronous calls in .net 4.5. `IAsyncResult` is rather complicated to be used and understood, and also is quite obsolete. – Uladzislaŭ Aug 07 '15 at 21:16
  • Thank you, your suggestion is better than an actual answer :) – JasonS Aug 07 '15 at 21:33
  • Ahh, I need to make synchronous code asynchronous, so I'll still need the "old slog" way, but will use async/await to call it. – JasonS Aug 07 '15 at 21:45
  • Why do you have such a requirement? Asynchronous calls are necessary only for IO operations and UI; otherwise you will gain no profit. (Nevertheless, you could use `Task.Run` to start some action on task pool and await it then) – Uladzislaŭ Aug 07 '15 at 21:48
  • I have code that interacts with a 3rd party COM library, and it currently locks up the UI when waiting on the COM component, so I'll be making an async wrapper over the top of it. will use async/await to call the wrapper though! – JasonS Aug 07 '15 at 21:51
  • 1
    Ok, it's reasonable for UI. You could use it like this: `var result = await Task.Run(() => comComponent.SomeAction())` – Uladzislaŭ Aug 07 '15 at 21:56
  • Thank you Vladislav! I also just found System.Threading.Tasks.Task.Factory.StartNew() so both will suffice! – JasonS Aug 07 '15 at 21:59
  • 1
    `Task.Run` is just a useful shortcut added in .net 4.5.1 for `Task.Factory.StartNew` with adequate arguments defaults. – Uladzislaŭ Aug 07 '15 at 22:02

3 Answers3

2

It's right on the page Calling Synchronous Methods Asynchronously that you link to

The second parameter is a user-defined object that passes information into the callback method.

It arrives via the IAsyncResult parameter's AsyncState property.

The BeginInvoke method signature is different for each delegate type. The language runtime guarantees it is defined with the same arguments as the delegate type plus two additional arguments which it discusses on the second page you've linked to. You've linked to the documentation for Func<T> which takes no parameters, but the example uses a custom delegate type that has two parameters, therefore the signatures are different.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
1

AsyncCallback delegate has signature equivalent to Action<object>. The second parameter of BeginInvoke will be provided as argument to your callback - some state to pass.

If your delegate has some paramters, they should be passed to the async invocation also. So BeginInvoke will have different signatures for different types of delegate.

In tutorial this one is used:

public delegate string AsyncMethodCaller(int callDuration, out int threadId);

So in invocation

IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);

3000 is value of callDuration, out threadId used for out int threadId, while null and null are callback and object.

Uladzislaŭ
  • 1,680
  • 10
  • 13
0

In the BeginInvoke object is your argument to pass into callback. It will be available as a property in the parameter for callback. I usually pass delegate itself, so I call EndInvoke on it.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
  • In my experience, the `IAsyncResult` parameter passed to the callback is always an instance of `AsyncResult`, which exposes a property `AsyncDelegate`, so there's no need to pass the delegate directly. See [this answer](http://stackoverflow.com/a/1484384/622391) for an example. – Simon MᶜKenzie Aug 09 '15 at 23:31