public void safeSave() { lock (writeLock) { try { this.context.SaveChanges(); } catch (Exception e) { BackgroundWork.messageRefreshHandlers(e.Message); System.Windows.MessageBox.Show(e.Message, "Error Loading"); } } /* TODO write with thread * * System.Threading.ThreadPool.QueueUserWorkItem( * (o) => { * lock (writeLock) * { * this.context.SaveChanges(); * } * }); * */ }
public void doRefresh() { try { //Call all registered handlers BackgroundWork.messageRefreshHandlers("Starting refresh..."); //Saved all input data in local variable var currentWifis = loadWifis(); var currentPlaces = DataManager.Instance.context.Places.Local.ToList(); if (currentWifis == null) { BackgroundWork.messageRefreshHandlers("Error: no wifi detected!"); return; } if (currentPlaces == null) { BackgroundWork.messageRefreshHandlers("No places stored!"); return; } //Run algorithm with retrieved input data Place newPlace = algo.computeCurrentPlace(currentWifis, currentPlaces); BackgroundWork.messageRefreshHandlers("Place Found"); //Update time counter - safe? //Also other write from UI events lock (writeLock) { //it is necessary to compute time //from last refesh, because it can be done manually if (this.currentState.Place == null) { this.oldDate = DateTime.Now; } DateTime now = DateTime.Now; TimeSpan diff = now.Subtract(oldDate); oldDate = now; newPlace.Cnt += (long)diff.TotalMilliseconds; //or simply //newPlace.Cnt = newPlace.Cnt + BackgroundWork.refreshTime / 1000; safeSave(); } if (currentState.Place == null || newPlace.PlaceId != currentState.Place.PlaceId) { currentState.Place = newPlace; var currentActions = currentState.Place.InActions.ToList(); BackgroundWork.placeChangedHandlers(newPlace); //System.Threading.Thread.Sleep(5000); foreach (models.Action a in currentActions) { //System.Threading.Thread.Sleep(10000); ActionManager.execute(a); } } BackgroundWork.messageRefreshHandlers("Done"); } catch (Exception) { BackgroundWork.messageRefreshHandlers("Failure."); } }