Multi threading in WCF RIA Services

Posted by synergetic on Stack Overflow See other posts from Stack Overflow or by synergetic
Published on 2010-12-21T05:26:33Z Indexed on 2010/12/21 5:31 UTC
Read the original article Hit count: 253

I use WCF RIA Services to update customer database. In domain service:

public void UpdateCustomer(Customer customer)
{
  this.ObjectContext.Customers.AttachAsModified(customer);
  syncCustomer(customer);
}

After update, a database trigger launches and depending on the columns updated it may insert a new record in CustomerChange table. syncCustomer(customer) method is executed to check for a new record in the CustomerChange table and if found it will create a text file which contains customer information and forwards that file to external system for import. Now this synchronization may take a time so I wanted to execute it in different thread. So:

private void syncCustomer(Customer customer)
{
  this.ObjectContext.SaveChanges();
  new Thread(() => syncCustomerInfo(customer.CustomerID)) 
                                            { IsBackground = true }.Start();
}

private void syncCustomerInfo(int customerID)
{
  //Thread.Sleep(2000);
  //does real job here ...
  ...
}

The problem is in most cases syncCustomerInfo method cannot find any new CustomerChange record even if it was definitely there. If I force thread sleep then it finds a new record. I also looked Entity Framework events but the only event provided by object context is SavingChanges which occur before changes are saved. Please suggest me what else to try.

© Stack Overflow or respective owner

Related posts about multithreading

Related posts about entity-framework