Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



C#
<pre>public class SchedHandler
    {
        public static List<Subject4Sched> SubSchedList = GetSchedSubList();
        public static int WeekSum = SubSchedList.Sum(x => x.WeekCount);
        public static int AssignSum = SubSchedList.Sum(x => x.Assigned);
        public static SchoolEntitiesProFinal ProDb;
        public static int WeekListsProgress = 0;
        public static List<SubWeekCount> GetSubList(int semId)
        {
            ProDb = new SchoolEntitiesProFinal();
            List<sp_GetSubWeekCountList_Result> subs = ProDb.sp_GetSubWeekCountList(semId).ToList();
            return subs.Select(item => new SubWeekCount(item.subid, item.subject, item.sessioncount)).ToList();
        }
        public List<SubWeekCount> SubList = GetSubList(1);

        //1- Insert Subjects With 0 Assigned
        public static void InsertSubjects(int subid,string subName, int week, int ass)
        {
            var sub = new tbl_schedule();
            sub.subid = subid;
            sub.subname = subName;
            sub.weekly = week;
            sub.assigned = ass;
           
            try
            {
                using (var db = new SchoolEntitiesProFinal())
                {
                    db.tbl_schedule.Add(sub);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void UpdtaeSubjects(int subid)
        {
            tbl_schedule sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);
            

            try
            {
                if (sub != null) sub.assigned = sub.assigned + 1;
                ProDb.SaveChanges();
            }
            catch (Exception)
            {
                MessageBox.Show("Error With Databse!!!");
            }
        }
        public static void RemoveSubjects(int subid)
        {
            tbl_schedule sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);
            try
            {
                if (sub!=null)
                {
                    if (sub.assigned == sub.weekly)
                    {
                        ProDb.tbl_schedule.Remove(sub);
                        ProDb.SaveChanges();
                    } 
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error With Databse!!!");
            }
        }
        public static void RemoveAllSubjects()
        {
            List<tbl_schedule> sub = ProDb.tbl_schedule.ToList();
            try
            {
                ProDb.tbl_schedule.RemoveRange(sub);
                ProDb.SaveChanges();
                
            }
            catch (Exception)
            {
                MessageBox.Show("Error With Databse!!!");
            }
        }
        public static int GetSubAssigned(int subid)
        {
            ProDb = new SchoolEntitiesProFinal();

            var sub = ProDb.tbl_schedule.FirstOrDefault(x => x.subid == subid);
            int count = 0;

            if (sub != null) count = sub.assigned;

            return count;
        }
        public static List<Subject4Sched> GetSchedSubList()
        {
            ProDb = new SchoolEntitiesProFinal();
            var subs = ProDb.tbl_schedule.ToList();
            return subs.Select(item => new Subject4Sched(item.subid, item.subname, item.weekly, item.assigned)).ToList();
        }
        public static Subject4Sched GetSub()
        {
            ProDb = new SchoolEntitiesProFinal();
            var f = ProDb.tbl_schedule.First();
            var sub = new Subject4Sched(f.subid, f.subname,f.weekly,f.assigned);
            return sub;
        }
        public static List<Sessions> WeekLists(int semId)
        {
            WeekListsProgress = 0;

            var sat = new List<Subject4Sched>();
            var sun = new List<Subject4Sched>();
            var mon = new List<Subject4Sched>();
            var tues = new List<Subject4Sched>();
            var wed = new List<Subject4Sched>();
            var thur = new List<Subject4Sched>();
            var fri = new List<Subject4Sched>();

            string[] weekEnds = File.ReadAllLines(@"C:\Program Files\School Manager\Pars Text\WeekEnds.txt");
            var holidaylist = weekEnds.Select(line => line.Split(',')).Select(tokens => tokens[0]).ToList();

            // Get Subjects For Semester 
            var subList = GetSubList(semId);
            RemoveAllSubjects();
            int v = 0;
            foreach (var sub in subList)
            {
                InsertSubjects(sub.SubId, sub.SubName, sub.WeekCount, 0);
                v += (20/subList.Count);
            }

            WeekListsProgress = 20;
            int weekDaysCount = 7 - holidaylist.Count;
            int sessionsCount = subList.Sum(x => x.WeekCount);
            var sessionsPerDay = Math.Ceiling(Convert.ToDouble(sessionsCount) / Convert.ToDouble(weekDaysCount));

            for (int x = 0; x < sessionsCount; x++)
            {
                foreach (var sched in SubSchedList.ToList())
                {
                    for (int i = 0; i < sched.WeekCount; i++)
                    {
                        if (i < sched.WeekCount)
                        {
                            if (sat.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Saturday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    sat.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount, sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++; 
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (sun.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Sunday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    sun.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (mon.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Monday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    mon.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (tues.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Tuesday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    tues.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (wed.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Wednesday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    wed.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (thur.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Thursday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    thur.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (i < sched.WeekCount)
                        {
                            if (fri.Count < sessionsPerDay && !holidaylist.Contains(DayOfWeek.Friday.ToString()))
                            {
                                int subCount = sat.Count(y => y.SubId == sched.SubId) +
                                               sun.Count(y => y.SubId == sched.SubId) +
                                               mon.Count(y => y.SubId == sched.SubId) +
                                               tues.Count(y => y.SubId == sched.SubId) +
                                               wed.Count(y => y.SubId == sched.SubId) +
                                               thur.Count(y => y.SubId == sched.SubId) +
                                               fri.Count(y => y.SubId == sched.SubId);
                                WeekListsProgress += 60 / sessionsCount;

                                if (subCount < sched.WeekCount)
                                {
                                    fri.Add(new Subject4Sched(sched.SubId, sched.SubName, sched.WeekCount,
                                        sched.Assigned++));
                                    UpdtaeSubjects(sched.SubId);
                                    i++;
                                    x++;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            WeekListsProgress = 80;

            int[] counts = {sat.Count, sun.Count, mon.Count, tues.Count, wed.Count, thur.Count, fri.Count};
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            var sList=new List<Sessions>();
            for (int i = 0; i < counts.Max(); i++)
            {
                string s1="";string s2 = "";string s3 = "";string s4 = ""; string s5 = ""; string s6 = ""; string s7 = "";
                if (sat.Count>i)
                {
                    s1 = sat[i].SubName;
                }
                if (sun.Count > i)
                {
                    s2 = sun[i].SubName;
                }
                if (mon.Count > i)
                {
                    s3 = mon[i].SubName;
                }
                if (tues.Count > i)
                {
                    s4 = tues[i].SubName;
                }
                if (wed.Count > i)
                {
                    s5 = wed[i].SubName;
                }
                if (thur.Count > i)
                {
                    s6 = thur[i].SubName;
                }
                if (fri.Count > i)
                {
                    s7 = fri[i].SubName;
                }
                
                sList.Add(new Sessions(i,s1,s2,s3,s4,s5,s6,s7));
                WeekListsProgress += 20/counts.Max();
            }
            WeekListsProgress = 100;
            return sList;
        }
       


    }
}


What I have tried:

C#
<pre>private void BackgroundThread()
        {
            this.Dispatcher.Invoke(() =>
            {
                sat.ItemsSource = SchedHandler.WeekLists(1);
            });

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_thread == null)
            {
                _thread = new Thread(new ThreadStart(BackgroundThread));
                _thread.IsBackground = true;
                _thread.Start();
            }

            pbWeekList.Value = SchedHandler.WeekListsProgress;
        }
Posted
Updated 22-Feb-21 7:02am

1 solution

 
Share this answer
 
Comments
Rabee3-F1.787545 22-Feb-21 15:37pm    
Thank you very much it was very helpful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900