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 .