I have the following scenario:
Client who is requesting a webservice to start
public bool Start(MyProject project, string error)
A web service who receives the call from the client in a method
public event EventHandler<StartEventArgs> startEvent;
public bool Start(MyProject project, string error)
{
Task<bool> result = StartAsync(project, error);
return result.Result;
}
protected virtual void OnStart(StartEventArgs e)
{
// thread safe trick, snapshot of event
var se = startEvent;
if (se != null)
{
startEvent(this, e);
}
}
private Task<bool> StartAsync(MyProject project, string error)
{
var taskCompletion = new TaskCompletionSource<bool>();
this.startEvent += (p, e) => taskCompletion.TrySetResult((e.Error == string.Empty) ? true : false);
this.OnStart(new StartEventArgs(project, error));
return taskCompletion.Task;
}
An application that is subscribing to an event that is located in the web service:
app.Start += EventHandler(App_Start)
private bool App_Start()
{
// does something
returns true/false
}
I want the web service to fire off the event in a Task, then wait for the function in the app.exe to finish and then return here to notify the user that the task has completed successfully.
I am not sure how to do this but in theory it would look something like this:
Task<bool> startTask = Task.Factory.StartNew(() => { OnStart() });
startTask.WaitAll(); // I think this is what I would need to for 4.0
return startTask.Result
I hope I am being descriptive enough for someone to see what I am trying to do. I would like the service to not have to know anything about the client and just run the task and once the event has finished its execution come back to this point and return to client a Boolean value representing success/failure.
Is this possible or am I taking a really wrong approach with this?
Update:
Obviously OnStart is not an event so how do I do what you are trying to explain to me?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…