private ManagedRequestBase popQueueElement() { if (requestFIFO != null && requestFIFO.Count > 0) { ManagedRequestBase result = requestFIFO.ElementAt(0); requestFIFO.Remove(result); return(result); } else { return(null); } }
public RepositoryManager(Logger logger) { SERVICES = new ServerConnection(logger); ioService = new IOService(); worker.DoWork += (o, ea) => { try { ManagedRequestBase mr = null; Application.Current.Dispatcher.Invoke(new Action(() => mr = popQueueElement())); if (mr != null) { processRequestFromQueue(mr); if (requestFIFO != null && requestFIFO.Count > 0) { System.Threading.Thread.Sleep(100); } } } catch (Exception) { } }; worker.RunWorkerCompleted += (o, ea) => { if (requestFIFO != null && requestFIFO.Count > 0) { worker.RunWorkerAsync(); } }; if (!worker.IsBusy) { worker.RunWorkerAsync(); } }
private void processRequestFromQueue(ManagedRequestBase currentlyManagedRequest) { var @switch = new Dictionary <Type, Action> { { typeof(ManagedRequestGeneral <Func <string, string, string, bool> >), () => { ManagedRequestGeneral <Func <string, string, string, bool> > cmr = (ManagedRequestGeneral <Func <string, string, string, bool> >)currentlyManagedRequest; bool result; switch (currentlyManagedRequest.MethodName.ToString()) { case "Login": result = cmr.MethodToCallWithParameters.Invoke("", "", ""); RaiseLoginEnded(result); break; } } }, { typeof(ManagedRequestGeneral <Func <GeneralAnswerData> >), () => { ManagedRequestGeneral <Func <GeneralAnswerData> > cmr = (ManagedRequestGeneral <Func <GeneralAnswerData> >)currentlyManagedRequest; GeneralAnswerData generalAnswerData; switch (currentlyManagedRequest.MethodName.ToString()) { case "GetCategories": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(); if (RaiseCategoriesQuerried != null) { RaiseCategoriesQuerried(generalAnswerData); } break; } } }, { typeof(ManagedRequestGeneral <Func <GeneralParameterData, GeneralAnswerData> >), () => { ManagedRequestGeneral <Func <GeneralParameterData, GeneralAnswerData> > cmr = (ManagedRequestGeneral <Func <GeneralParameterData, GeneralAnswerData> >)currentlyManagedRequest; GeneralAnswerData generalAnswerData; switch (currentlyManagedRequest.MethodName.ToString()) { case "AddItem": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseItemAdded != null) { RaiseItemAdded(generalAnswerData); } break; case "AddCategory": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseCategoryAdded != null) { RaiseCategoryAdded(generalAnswerData); } break; case "AddPlan": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaisePlanAdded != null) { RaisePlanAdded(generalAnswerData); } break; case "DeleteItem": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseItemDeleted != null) { RaiseItemDeleted(generalAnswerData); } break; case "DeleteCategory": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseCategoryDeleted != null) { RaiseCategoryDeleted(generalAnswerData); } break; case "DeletePlan": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaisePlanDeleted != null) { RaisePlanDeleted(generalAnswerData); } break; case "UpdateItem": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseItemSaved != null) { RaiseItemSaved(generalAnswerData); } break; case "UpdateCategory": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseCategorySaved != null) { RaiseCategorySaved(generalAnswerData); } break; case "UpdatePlan": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaisePlanSaved != null) { RaisePlanSaved(generalAnswerData); } break; case "GetPlansAndExpensesSummary": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaisePlansAndExpensesSummaryQuerried != null) { RaisePlansAndExpensesSummaryQuerried(generalAnswerData); } break; case "GetDailyCategoryExpensesSummary": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(null); if (RaiseDailyCategoryExpensesSummaryQuerried != null) { RaiseDailyCategoryExpensesSummaryQuerried(generalAnswerData); } break; } } }, { typeof(ManagedRequestGeneral <Func <bool, bool, bool, GeneralAnswerData> >), () => { ManagedRequestGeneral <Func <bool, bool, bool, GeneralAnswerData> > cmr = (ManagedRequestGeneral <Func <bool, bool, bool, GeneralAnswerData> >)currentlyManagedRequest; GeneralAnswerData generalAnswerData; switch (currentlyManagedRequest.MethodName.ToString()) { case "GetItems": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(true, true, false); if (RaiseItemsQuerried != null) { RaiseItemsQuerried(generalAnswerData); } break; case "GetPlans": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(false, true, true); if (RaisePlansQuerried != null) { RaisePlansQuerried(generalAnswerData); } break; case "GetSummaries": generalAnswerData = cmr.MethodToCallWithParameters.Invoke(false, true, false); if (RaiseSummariesQuerried != null) { RaiseSummariesQuerried(generalAnswerData); } break; } } }, { typeof(ManagedRequestGeneral <Action>), () => { ManagedRequestGeneral <Action> cmr = (ManagedRequestGeneral <Action>)currentlyManagedRequest; switch (currentlyManagedRequest.MethodName.ToString()) { case "Logout": cmr.MethodToCallWithParameters.Invoke(); RaiseLogoutEnded(); break; } } }, }; Type type = currentlyManagedRequest.GetType(); @switch[type](); }