0

This is my current implementation . I use global session variables to pass data from begin method to the function that does all the work.

string parameter1;
string parameter2;
public IAsyncResult BeginSomeMethod(string parameter1, string parameter2, AsyncCallback callback, object state)
{
    this.parameter1 = parameter1;
    this.parameter2 = parameter2;
    var task = Task<string>.Factory.StartNew(my_function, state);
    return task.ContinueWith(res => callback(task));
}
public string EndSomeMethod(IAsyncResult result)
{
    Console.WriteLine("If i use parameters when calling my_function, this message will never be displayed . ");
    return ((Task<string>)result).Result;
}
private string my_function(object state)
{

    Task.Delay(5000);

    return parameter1 + " " + parameter2;
}

The problem with this implementation is that if my_function execution takes too long, the parameter1 or parameter2 variables can be changed by another call and it messes up everything .

So, I just want to pass parameter1 and parameter 2 along with Factory.StartNew instead of using global variables but so far i didn't manage to .

F Andrei
  • 668
  • 2
  • 9
  • 24
  • see [here](http://stackoverflow.com/questions/8127316/passing-a-method-parameter-using-task-factory-startnew) – default Aug 12 '14 at 07:34
  • I tried that already . The parameters are passed to my_function, the code is executed but the main method doesn't return anything ... I get timed out exception when calling it from the client . Somehow, it never reaches this line from EndSomeMethod: return ((Task)result).Result; – F Andrei Aug 12 '14 at 07:48
  • I thought your question was about passing parameters to `my_function`. What exactly is the issue you are having? Note that you can `edit` your question via the edit button below your question. – default Aug 12 '14 at 08:46
  • The problem is if I pass parameters like you suggested , SomeMethod won't return anything ever . The call from the WCF client: client.SomeMethod(param1,param2); gets no response and eventually times out . – F Andrei Aug 12 '14 at 09:19
  • I edited the code and added this: Console.WriteLine("If i use parameters when calling my_function, this message will never be displayed . "); – F Andrei Aug 12 '14 at 09:23
  • you're not showing `SomeMethod`. Also, you are never starting `SomeMethod`. You are however starting `my_function`. You will probably have to provide a [sscce](http://sscce.org) – default Aug 12 '14 at 09:43
  • This is an async implementation of a WCF OperationContract . You have BeginSomeMethod and EndSomeMethod there and my_function does all the job . In the form above it works perfectly ... however if i send parameters with the task, it will stop returning the result on client side. – F Andrei Aug 12 '14 at 10:00

1 Answers1

0

Here's the solution:

    public IAsyncResult BeginSomeMethod(string parameter1, string parameter2, AsyncCallback callback, object state)
    {

        var task = Task<List<int>>.Factory.StartNew((res) => my_function(state, parameter1, parameter2), state);
        return task.ContinueWith(res => callback(task));
    }

    public List<int> EndSomeMethod(IAsyncResult result)
    {
        return ((Task<List<int>>)result).Result;
    }

    private List<int> my_function(object state, string parameter1, string parameter2)
    {
        //implementation goes here
    }
F Andrei
  • 668
  • 2
  • 9
  • 24