Reputation: 65
I am looking for an efficient solution for my problems. I am using VS 2010. I want to do a series of operations from wcf service method and send back the each operations status to the calling client. I have setup wcf with callback contract and using duplex channel, I am able to connect to wcf. When I start the long running operation, sometimes, it will trigger the call back sometimes it is not. I dont know why. Below is the method I was following.
In the wcf service method,
public void Start()
{
List<Employee> empLists = GetEmpData(); // geting lots of employee objects
foreach(Employee emp in empLists) // maybe 1000 records
{
StartlongRunning(emp);
}
}
private void StartlongRunning(Employee emp)
{
// here i am creating a new background worker...
// Here i am registering for RunWorkerCompleted, DoWork, ReportProgress events...
bgw.RunWorkerAsync(emp)
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
Employee emp = (Employee)e.Argument;
using (ClassA p = new ClassA(emp.ID)) // this class is from another dll.
{
e.Result = p.StartProcess(emp.Code);
}
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// This is not calling properly.
// Sometimes this method is working...most of the time it is not working..
// here i am unregistering the registerd DoWork, RunWorkerCompleted events...
// calling bgw.Dispose(); // i tried without this also;...but no use...
// from here I am firing the callback to client...
}
Here is the implementation of the StartProcess method.
public string StartProcess(string empcode)
{
// call to another method1() // here saving to DB frequently. works fine
// call to another method2() // here also saving to DB frequently. works fine
// call to someother method3() // here also some DB insert frequently. fine
// call to Method4() // here also some DB insert.
// this method is not calling frequently..
// sometimes it is calling but most of times not..why ???
return value;
}
private void SaveToDB(args1, args2...)
{
DatabaseHelper.Save(args1, args2.....); // this static class is from another dll
// only DB operation in this static class..
}
The implemenation of this static class is as shown below.
using (SqlConnection conn = new SqlConnection(DBConnection))
{
conn.open;
using (SqlCommand cmd = conn.CreateCommand())
{
...adding parameters
cmd.ExecuteNonQuery();
}
conn.close();
}
If the StartProcess
returns, then the background worker will execute the RunWorker
method.
But it is not happening. What is wrong in here?
I am creating each ClassA
object for each background worker.
And once an object of ClassA
is completed, then it will be disposed.
But i don't know why the StartProcess
is not returning the call properly.
In my implementation, is there overlapping of ClassA
objects between background workers ??
Upvotes: 0
Views: 229
Reputation: 1418
If you want to create application to work with several backgroundWorkers in the same time you should initialize new backgroundWorker object for each operations you are doing.. That will solve the problem.
Upvotes: 0
Reputation: 4564
I think the problem is that you call RunWorkerAsync in a loop without checking that it is not busy. You should call RunWorkerAsync with the list as the parameter rather than trying to start all the work in different threads.
I would do something like this:
public void Start()
{
List<Employee> empLists = GetEmpData(); // geting lots of employee objects
StartlongRunning(empLists);
}
And change bgw_DoWork accordingly.
If you need to see the progress you could call ReportProgress for each employee object.
Upvotes: 1