Question

I have a WP8 app that has multiple (at times, up to 40) threads that have to get the data from a webservice and then commit to a localdb.

I have implemented an AutoResetEvent-based pattern where each Repository method looks somewhat like this:

 public class MySuperAppRepository
  {
   public static AutoResetEvent DataAccess = new AutoResetEvent(true);

   public MyFancyObject CreateMyFancyObject(string path, int something)
   {
    DataAccess.WaitOne();
    try
    {
      using (var dbContext = new   MySuperAppDataContext(MySuperAppDataContext.DbConnectionString))
      {
        var mfo = new MyFancyObject();
        dbContext.MyFancyObjects.InsertOnSubmit(mfo);
        mfo.Path = path;
        mfo.Something = something;
        dbContext.SubmitChanges();
        return mfo;
      }
    }
    finally
    {
      DataAccess.Set();
    }
  }
}

This is all nice and clean, but as soon as i get multiple threads (as mentioned above), the performance is PATHETIC. i can get lots of requests come down and then they're all waiting for db to be free.

Is there a better alternative? Would using lock(object) improve the performance?

Was it helpful?

Solution

Can you try not creating a new DataContext on every data operation.

Also try out some of the best practices mentioned here.

in particular :

Enabling fast updates with a version column One of the easiest ways to optimize the performance of an update operation on a table is to add a version column. This optimization is specific to LINQ to SQL for Windows Phone. For example, in an entity, add the following code.

    [Column(IsVersion=true)]
    private Binary _version;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top