c# - 在函数执行期间将进度条与函数中的值链接
问题描述
我已经创建了一个带有公共函数的类在执行期间返回一个列表公共int在函数结束时增加到100这个进度条的值在函数期间增加如何在类中的公共int和wpf中的进度条之间进行同步最后的形式我想要 sList 作为结果和 WeekListsProgress 作为进度条 vlaue
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;
}
}
}
解决方案
https://www.youtube.com/watch?v=zQMNFEz5IVU&t=1s
private async void Button_Click(object sender, RoutedEventArgs e)
{
var sl = new List<Sessions>();
var progress = new Progress<int>(value =>
{
pbWeekList.Value =value ;
});
await Task.Run(() => sl= SchedHandler.WeekLists(1, progress));
dg.ItemsSource = sl;
}
但首先你必须改为
public static List<Sessions> WeekLists(int semId,IProgress<int> progress)
{
WeekListsProgress = 0;
progress.Report(WeekListsProgress);
/////////////////////
/////////////////
////////////////
Thread.Sleep(100);
WeekListsProgress = 100;
progress.Report(WeekListsProgress);
return sList;
}
推荐阅读
- python-3.x - 我不明白为什么这个值在 leetcode 上会发生变化?
- r - 在 r 中使用颜色和属性符号系统信息编写分类栅格
- go - 将执行文本/模板模板的结果分配给变量
- reactjs - 如何防止反应原生键盘上的按键事件?
- python - 在 python 中的 xarray 上应用 R-function qmap 加速
- backup - 从第 N 个字节开始复制和覆盖文件
- javascript - 为什么我不能将我的道具发送到组件?
- html - 更改相对于父 div 的字体大小。使用引导程序
- c# - 如何在检查唯一值的同时将数据从一个表单插入到另一个表单的列表框中?
- r - 如何改变R中igraph中边缘的强度和对称性?