Rabih Nasr

Rabih Nasr

  • NA
  • 74
  • 945

Link progress bar with value in function during function execution

Feb 22 2021 11:31 AM
I've Created a class with public function returns a list during execution public int increasing to 100 at the end of function this value for progress bar is increasing during function how to make synchronization between public int in the class and the progress bar in the wpf form at the end I want the sList as result and WeekListsProgress as progressbar vlaue is changing during WeekLists execute
  1. public class SchedHandler  
  2. {  
  3. public static List<Subject4Sched> SubSchedList = GetSchedSubList();  
  4. public static int WeekSum = SubSchedList.Sum(x => x.WeekCount);  
  5. public static int AssignSum = SubSchedList.Sum(x => x.Assigned);  
  6. public static SchoolEntitiesProFinal ProDb;  
  7. public static int WeekListsProgress = 0;  
  8. public static List<SubWeekCount> GetSubList(int semId)  
  9. {  
  10. ProDb = new SchoolEntitiesProFinal();  
  11. List<sp_GetSubWeekCountList_Result> subs = ProDb.sp_GetSubWeekCountList(semId).ToList();  
  12. return subs.Select(item => new SubWeekCount(item.subid, item.subject, item.sessioncount)).ToList();  
  13. }  
  14. public List<SubWeekCount> SubList = GetSubList(1);  
  15. //1- Insert Subjects With 0 Assigned  
  16. public static void InsertSubjects(int subid,string subName, int week, int ass)  
  17. {  
  18. var sub = new tbl_schedule();  
  19. sub.subid = subid;  
  20. sub.subname = subName;  
  21. sub.weekly = week;  
  22. sub.assigned = ass;  
  23. try  
  24. {  
  25. using (var db = new SchoolEntitiesProFinal())  
  26. {  
  27. db.tbl_schedule.Add(sub);  
  28. db.SaveChanges();  
  29. }  
  30. }  
  31. catch (Exception ex)  
  32. {  
  33. MessageBox.Show(ex.Message);  
  34. }  
  35. }  
  36. public static void UpdtaeSubjects(int subid)  
  37. {  
  38. tbl_schedule sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);  
  39. try  
  40. {  
  41. if (sub != null) sub.assigned = sub.assigned + 1;  
  42. ProDb.SaveChanges();  
  43. }  
  44. catch (Exception)  
  45. {  
  46. MessageBox.Show("Error With Databse!!!");  
  47. }  
  48. }  
  49. public static void RemoveSubjects(int subid)  
  50. {  
  51. tbl_schedule sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);  
  52. try  
  53. {  
  54. if (sub!=null)  
  55. {  
  56. if (sub.assigned == sub.weekly)  
  57. {  
  58. ProDb.tbl_schedule.Remove(sub);  
  59. ProDb.SaveChanges();  
  60. }  
  61. }  
  62. }  
  63. catch (Exception)  
  64. {  
  65. MessageBox.Show("Error With Databse!!!");  
  66. }  
  67. }  
  68. public static void RemoveAllSubjects()  
  69. {  
  70. List<tbl_schedule> sub = ProDb.tbl_schedule.ToList();  
  71. try  
  72. {  
  73. ProDb.tbl_schedule.RemoveRange(sub);  
  74. ProDb.SaveChanges();  
  75. }  
  76. catch (Exception)  
  77. {  
  78. MessageBox.Show("Error With Databse!!!");  
  79. }  
  80. }  
  81. public static int GetSubAssigned(int subid)  
  82. {  
  83. ProDb = new SchoolEntitiesProFinal();  
  84. var sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);  
  85. int count = 0;  
  86. if (sub != null) count = sub.assigned;  
  87. return count;  
  88. }  
  89. public static List<Subject4Sched> GetSchedSubList()  
  90. {  
  91. ProDb = new SchoolEntitiesProFinal();  
  92. var subs = ProDb.tbl_schedule.ToList();  
  93. return subs.Select(item => new Subject4Sched(item.subid, item.subname, item.weekly, item.assigned)).ToList();  
  94. }  
  95. public static Subject4Sched GetSub()  
  96. {  
  97. ProDb = new SchoolEntitiesProFinal();  
  98. var f = ProDb.tbl_schedule.First();  
  99. var sub = new Subject4Sched(f.subid, f.subname,f.weekly,f.assigned);  
  100. return sub;  
  101. }  
  102. public static List<Sessions> WeekLists(int semId)  
  103. {  
  104. WeekListsProgress = 0;  
  105. var sat = new List<Subject4Sched>();  
  106. var sun = new List<Subject4Sched>();  
  107. var mon = new List<Subject4Sched>();  
  108. var tues = new List<Subject4Sched>();  
  109. var wed = new List<Subject4Sched>();  
  110. var thur = new List<Subject4Sched>();  
  111. var fri = new List<Subject4Sched>();  
  112. string[] weekEnds = File.ReadAllLines(@"C:\Program Files\School Manager\Pars Text\WeekEnds.txt");  
  113. var holidaylist = weekEnds.Select(line => line.Split(',')).Select(tokens => tokens[0]).ToList();  
  114. // Get Subjects For Semester  
  115. var subList = GetSubList(semId);  
  116. RemoveAllSubjects();  
  117. int v = 0;  
  118. foreach (var sub in subList)  
  119. {  
  120. InsertSubjects(sub.SubId, sub.SubName, sub.WeekCount, 0);  
  121. v += (20/subList.Count);  
  122. }  
  123. WeekListsProgress = 20;  
  124. int weekDaysCount = 7 - holidaylist.Count;  
  125. int sessionsCount = subList.Sum(x => x.WeekCount);  
  126. var sessionsPerDay = Math.Ceiling(Convert.ToDouble(sessionsCount) / Convert.ToDouble(weekDaysCount));  
  127. for (int x = 0; x < sessionsCount; x++)  
  128. {  
  129. foreach (var sched in SubSchedList.ToList())  
  130. {  
  131. for (int i = 0; i < sched.WeekCount; i++)  
  132. {  
  133. if (i < sched.WeekCount)  
  134. {  
  135. if (sat.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Saturday.ToString()))  
  136. {  
  137. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  138. sun.Count(y => y.SubId == sched.SubId) +  
  139. mon.Count(y => y.SubId == sched.SubId) +  
  140. tues.Count(y => y.SubId == sched.SubId) +  
  141. wed.Count(y => y.SubId == sched.SubId) +  
  142. thur.Count(y => y.SubId == sched.SubId) +  
  143. fri.Count(y => y.SubId == sched.SubId);  
  144. WeekListsProgress += 60 / sessionsCount;  
  145. if (subCount < sched.WeekCount)  
  146. {  
  147. sat.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount, sched.Assigned++));  
  148. UpdtaeSubjects(sched.SubId);  
  149. i++;  
  150. x++;  
  151. }  
  152. }  
  153. }  
  154. else  
  155. {  
  156. break;  
  157. }  
  158. if (i < sched.WeekCount)  
  159. {  
  160. if (sun.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Sunday.ToString()))  
  161. {  
  162. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  163. sun.Count(y => y.SubId == sched.SubId) +  
  164. mon.Count(y => y.SubId == sched.SubId) +  
  165. tues.Count(y => y.SubId == sched.SubId) +  
  166. wed.Count(y => y.SubId == sched.SubId) +  
  167. thur.Count(y => y.SubId == sched.SubId) +  
  168. fri.Count(y => y.SubId == sched.SubId);  
  169. WeekListsProgress += 60 / sessionsCount;  
  170. if (subCount < sched.WeekCount)  
  171. {  
  172. sun.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  173. sched.Assigned++));  
  174. UpdtaeSubjects(sched.SubId);  
  175. i++;  
  176. x++;  
  177. }  
  178. }  
  179. }  
  180. else  
  181. {  
  182. break;  
  183. }  
  184. if (i < sched.WeekCount)  
  185. {  
  186. if (mon.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Monday.ToString()))  
  187. {  
  188. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  189. sun.Count(y => y.SubId == sched.SubId) +  
  190. mon.Count(y => y.SubId == sched.SubId) +  
  191. tues.Count(y => y.SubId == sched.SubId) +  
  192. wed.Count(y => y.SubId == sched.SubId) +  
  193. thur.Count(y => y.SubId == sched.SubId) +  
  194. fri.Count(y => y.SubId == sched.SubId);  
  195. WeekListsProgress += 60 / sessionsCount;  
  196. if (subCount < sched.WeekCount)  
  197. {  
  198. mon.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  199. sched.Assigned++));  
  200. UpdtaeSubjects(sched.SubId);  
  201. i++;  
  202. x++;  
  203. }  
  204. }  
  205. }  
  206. else  
  207. {  
  208. break;  
  209. }  
  210. if (i < sched.WeekCount)  
  211. {  
  212. if (tues.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Tuesday.ToString()))  
  213. {  
  214. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  215. sun.Count(y => y.SubId == sched.SubId) +  
  216. mon.Count(y => y.SubId == sched.SubId) +  
  217. tues.Count(y => y.SubId == sched.SubId) +  
  218. wed.Count(y => y.SubId == sched.SubId) +  
  219. thur.Count(y => y.SubId == sched.SubId) +  
  220. fri.Count(y => y.SubId == sched.SubId);  
  221. WeekListsProgress += 60 / sessionsCount;  
  222. if (subCount < sched.WeekCount)  
  223. {  
  224. tues.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  225. sched.Assigned++));  
  226. UpdtaeSubjects(sched.SubId);  
  227. i++;  
  228. x++;  
  229. }  
  230. }  
  231. }  
  232. else  
  233. {  
  234. break;  
  235. }  
  236. if (i < sched.WeekCount)  
  237. {  
  238. if (wed.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Wednesday.ToString()))  
  239. {  
  240. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  241. sun.Count(y => y.SubId == sched.SubId) +  
  242. mon.Count(y => y.SubId == sched.SubId) +  
  243. tues.Count(y => y.SubId == sched.SubId) +  
  244. wed.Count(y => y.SubId == sched.SubId) +  
  245. thur.Count(y => y.SubId == sched.SubId) +  
  246. fri.Count(y => y.SubId == sched.SubId);  
  247. WeekListsProgress += 60 / sessionsCount;  
  248. if (subCount < sched.WeekCount)  
  249. {  
  250. wed.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  251. sched.Assigned++));  
  252. UpdtaeSubjects(sched.SubId);  
  253. i++;  
  254. x++;  
  255. }  
  256. }  
  257. }  
  258. else  
  259. {  
  260. break;  
  261. }  
  262. if (i < sched.WeekCount)  
  263. {  
  264. if (thur.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Thursday.ToString()))  
  265. {  
  266. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  267. sun.Count(y => y.SubId == sched.SubId) +  
  268. mon.Count(y => y.SubId == sched.SubId) +  
  269. tues.Count(y => y.SubId == sched.SubId) +  
  270. wed.Count(y => y.SubId == sched.SubId) +  
  271. thur.Count(y => y.SubId == sched.SubId) +  
  272. fri.Count(y => y.SubId == sched.SubId);  
  273. WeekListsProgress += 60 / sessionsCount;  
  274. if (subCount < sched.WeekCount)  
  275. {  
  276. thur.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  277. sched.Assigned++));  
  278. UpdtaeSubjects(sched.SubId);  
  279. i++;  
  280. x++;  
  281. }  
  282. }  
  283. }  
  284. else  
  285. {  
  286. break;  
  287. }  
  288. if (i < sched.WeekCount)  
  289. {  
  290. if (fri.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Friday.ToString()))  
  291. {  
  292. int subCount = sat.Count(y => y.SubId == sched.SubId) +  
  293. sun.Count(y => y.SubId == sched.SubId) +  
  294. mon.Count(y => y.SubId == sched.SubId) +  
  295. tues.Count(y => y.SubId == sched.SubId) +  
  296. wed.Count(y => y.SubId == sched.SubId) +  
  297. thur.Count(y => y.SubId == sched.SubId) +  
  298. fri.Count(y => y.SubId == sched.SubId);  
  299. WeekListsProgress += 60 / sessionsCount;  
  300. if (subCount < sched.WeekCount)  
  301. {  
  302. fri.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,  
  303. sched.Assigned++));  
  304. UpdtaeSubjects(sched.SubId);  
  305. i++;  
  306. x++;  
  307. }  
  308. }  
  309. }  
  310. else  
  311. {  
  312. break;  
  313. }  
  314. }  
  315. }  
  316. }  
  317. WeekListsProgress = 80;  
  318. int[] counts = {sat.Count, sun.Count, mon.Count, tues.Count, wed.Count, thur.Count, fri.Count};  
  319. ////////////////////////////////////////////////////////////////////////////////////////////////////  
  320. var sList=new List<Sessions>();  
  321. for (int i = 0; i < counts.Max(); i++)  
  322. {  
  323. string s1="";string s2 = "";string s3 = "";string s4 = ""string s5 = ""string s6 = ""string s7 = "";  
  324. if (sat.Count>i)  
  325. {  
  326. s1 = sat[i].SubName;  
  327. }  
  328. if (sun.Count > i)  
  329. {  
  330. s2 = sun[i].SubName;  
  331. }  
  332. if (mon.Count > i)  
  333. {  
  334. s3 = mon[i].SubName;  
  335. }  
  336. if (tues.Count > i)  
  337. {  
  338. s4 = tues[i].SubName;  
  339. }  
  340. if (wed.Count > i)  
  341. {  
  342. s5 = wed[i].SubName;  
  343. }  
  344. if (thur.Count > i)  
  345. {  
  346. s6 = thur[i].SubName;  
  347. }  
  348. if (fri.Count > i)  
  349. {  
  350. s7 = fri[i].SubName;  
  351. }  
  352. sList.Add(new Sessions(i,s1,s2,s3,s4,s5,s6,s7));  
  353. WeekListsProgress += 20/counts.Max();  
  354. }  
  355. WeekListsProgress = 100;  
  356. return sList;  
  357. }  

Attachment: SchedHandler.rar

Answers (1)