C#线程应用程序出错
我已经创建了4个在线考试时间监控项目C#线程应用程序出错,c#,winforms,multithreading,C#,Winforms,Multithreading,我已经创建了4个在线考试时间监控项目 ASP.NET在考试中的应用 用于业务流程和数据访问的类库 类库到数据库执行 Windows窗体应用程序监控考试时间 在Windows窗体应用程序中,当新的考试开始时,我使用线程进行监控,我想在5次考试结束前发出通知 使用visual studio调试应用程序时,不会出现任何错误。但是手动单击.exe文件并运行应用程序,它会收到一个错误“应用程序停止工作” 这是我的windows窗体应用程序代码 public partial class Form1 : Fo
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread t;
Thread t1;
private void Form1_Load(object sender, EventArgs e)
{
fillList();
CheckForIllegalCrossThreadCalls = false;
t= new Thread(()=>getTrigger());
t1 = new Thread(() => TimeOption());
t.Start();
t1.Start();
// getTrigger();
}
private void getTrigger()
{
int temp = StudentExamDB.getPendingCount();
while (true)
{
if (temp != StudentExamDB.getPendingCount())
{
fillList();
temp = StudentExamDB.getPendingCount();
}
}
}
List<string> added = new List<string>();
private void TimeOption()
{
while(true)
{
DataTable dt = StudentExamDB.getFinishingList();
foreach (DataRow dr in dt.Rows)
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["enrollmentid"].Value.ToString() == dr["enrollmentid"].ToString())
{
if (added.Contains(dr["enrollmentid"].ToString()))
{
}
else
{
notifyIcon1.BalloonTipTitle = "Ending Some Examinations";
notifyIcon1.BalloonTipText = "click here to show more details about examination time";
notifyIcon1.ShowBalloonTip(5000);
added.Add(dr["enrollmentid"].ToString());
}
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Tomato;
dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
}
}
}
catch
{
}
}
}
}
private void fillList()
{
try
{
dataGridView1.DataSource = StudentExamDB.getPendingList();
}
catch
{
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
t1.Abort();
}
private void setToFinishedToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StudentExamDB.updateStatus(int.Parse(dataGridView1.CurrentRow.Cells["enrollmentid"].Value.ToString()));
fillList();
}
catch
{
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
螺纹t;
螺纹t1;
私有void Form1\u加载(对象发送方、事件参数e)
{
填充列表();
CheckForIllegalCrossThreadCalls=false;
t=新线程(()=>getTrigger());
t1=新线程(()=>TimeOption());
t、 Start();
t1.Start();
//getTrigger();
}
private void getTrigger()
{
int temp=StudentExamDB.getPendingCount();
while(true)
{
if(temp!=StudentExamDB.getPendingCount())
{
填充列表();
temp=StudentExamDB.getPendingCount();
}
}
}
添加的列表=新列表();
私有void TimeOption()
{
while(true)
{
DataTable dt=StudentExamDB.getFinishingList();
foreach(数据行dr在dt.行中)
{
尝试
{
对于(int i=0;i
您知道这是怎么回事吗
CheckForIllegalCrossThreadCalls = false;
你明确地关闭了对你做错了什么的检查。这意味着您知道不应该在非UI线程上修改UI,但无论如何您都在这样做。如果没有这行代码,在VisualStudio中运行时肯定会出现异常
这至少是问题之一。您的TimeOption
方法正在非UI线程上运行,但它正在修改UI。别那么做。还有各种其他选项,包括BackgroundWorker
和Control.Invoke/BeginInvoke
然后
- 在
和TimeOption
中都有紧密的循环。基本上,你将永远地对数据库进行重击。那真是个坏主意。您至少应该在迭代之间有一个延迟getTrigger
- 到处都是空的
块:catch
。这基本上是说,无论发生什么错误,你都很好——你可以继续,忽略它,甚至不记录发生了什么。不要那样做catch{}
- 您正在使用
终止线程。不要那样做。在这种情况下,使用volatileThread.Abort
字段指示您希望何时完成,并在循环的每次迭代中检查它是非常简单的bool
我怀疑这些问题是由于您从另一个线程不恰当地访问UI造成的,但我不能肯定。修复以上所有问题,您将有一个更好的代码库来诊断仍然存在的任何问题。您知道这会起什么作用吗
CheckForIllegalCrossThreadCalls = false;
你明确地关闭了对你做错了什么的检查。这意味着您知道不应该在非UI线程上修改UI,但无论如何您都在这样做。如果没有这行代码,在VisualStudio中运行时肯定会出现异常
这至少是问题之一。您的TimeOption
方法正在非UI线程上运行,但它正在修改UI。别那么做。还有各种其他选项,包括BackgroundWorker
和Control.Invoke/BeginInvoke
然后
- 在
和TimeOption
中都有紧密的循环。基本上,你将永远地对数据库进行重击。那真是个坏主意。您至少应该在迭代之间有一个延迟getTrigger
- 你有空的
c