Title: Task.Factory.FromAsync never calls the end method
Tags: c#;asynchronous;task;factory
Question: I'm kind of stumped and have been staring/debugging this code for hours now.

In my service I have -

```var task = Task.Factory.FromAsync(
    AnotherService.BeginMethod(arg1, null null),
    AnotherService.EndMethod,
    TaskCreationOptions.None)

task.ContinueWith((antecedent) =&gt;
{
    if (antecedent.isFaulted....)
    else // do something else
}
```

I have the above code wrapped in a ```TaskCompletionSource``` and set the result/exception in the ```task.ContinueWith``` method. So far, so good.

The problem -
As I'm debugging my unit tests (I have mocks for AnotherService), The begin is called, I store the variable and set the result on the tcs in my mock service. but my ```EndMethod``` in ```MockAnotherService``` is never called.  

I assumed that the tcs returned from the mock service would get signaled when I set result/exception on it, causing the ```From.Asyn```c call to call my End method. Is this not the case?

EDIT -

My mock implementation -

```    public IAsyncResult BeginSetDevice(Device device, AsyncCallback callback, object state)
    {
        var tcs = new TaskCompletionSource<string&gt;(state);
        var setTask = Task.Factory.StartNew(
            () =&gt;
            {
                if (this.FaultedState)
                {
                    tcs.SetException(new Exception("You asked for a fault"));
                }
                else
                {
                    this.DeviceToReturn = device;
                    tcs.SetResult("success");
                }
            });

        return tcs.Task;
    }
```

Comment: Added the mock implementation. I can debug and confirm that tcs.setResult is called and tcs status is changed to RanToCompletion

Comment: Never mind. I was missing a callback in my mock implementation.. That seems to fix it. Looks like the FromAsync method fills its own callback method. Thanks.

            

`if (callback != null)`
`{`
`callback(tcs.Task);`
`}`

Comment: `FromAsync` waits for the `IAsyncResult` returned by `AnotherService.BeginMethod` to be signalled. Could you show the code for `MockAnotherService`?

Comment: Everything in your question looks correct.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:11/03/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c#><asynchronous><task><factory>', 'Title': 'Task.Factory.FromAsync never calls the end method', 'ViewCount': 1558, 'id': 7999213, 'user_AboutMe': '', 'user_AccountId': 294029.0, 'user_DisplayName': 'kosh', 'user_Reputation': 551.0, 'user_Views': 39.0, 'user_id': 597669.0}}