I have this method
public override SockResponse Process(Socket socket, Client client) {
CallResponse rsp = new CallResponse();
try
{
using (Transact X = client.Session.NewTransaction())
{
foreach (CallData call in Payload)
{
DataCall dataCall = new DataCall();
SqlDataReader rdr = dataCall.Execute(X, call);
rsp.Result.Add(dataCall.BuildResult(rdr));
rdr.Close();
}
rsp.ErrMsg = "";
X.Commit();
}
}
catch (Exception err)
{
rsp.ErrMsg = err.Message;
return rsp;
}
return rsp;
}
and now I want to make it work asynchronously, and for that I've tried using BackgroundWorker
, the new code looks like this
public override SockResponse Process(Socket socket, Client client)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(
delegate(object sender, DoWorkEventArgs e)
{
CallResponse rsp = new CallResponse();
try
{
using (Transact X = client.Session.NewTransaction())
{
foreach (CallData call in Payload)
{
DataCall dataCall = new DataCall();
SqlDataReader rdr = dataCall.Execute(X, call);
rsp.Result.Add(dataCall.BuildResult(rdr));
rdr.Close();
}
rsp.ErrMsg = "";
X.Commit();
}
}
catch (Exception err)
{
rsp.ErrMsg = err.Message;
e.Result = rsp;
}
e.Result = rsp;
});
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
delegate(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
Log.Logger.Error(e.Error.Message);
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
Log.Logger.Info("CALL process was cancelled");
}
else
{
// Then, handle the result
return e.Result; // this is where the problem is
}
});
worker.RunWorkerAsync();
}
The problem is that I have no idea on how to return the result when the worker
is done