[英]Error in C# threading application
I have created 4 projects to online examination time monitoring 我创建了4个在线考试时间监控项目
in Windows form application I use threads for monitor when new exam start and I want to indicate notification before 5 munites to end the examination. 在Windows表单应用程序中,我在新的考试开始时使用线程进行监控,我想在5个munites之前指示通知以结束考试。
when using visual studio for debug the app its not getting any error. 当使用visual studio调试应用程序时,它没有收到任何错误。 but manually click the .exe file and run the application it getting an error "application stopped working"
但手动单击.exe文件并运行应用程序它会收到错误“应用程序停止工作”
This is my code for windows form application 这是我的Windows窗体应用程序的代码
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
{
}
}
}
Are you aware of what this does? 你知道这是做什么的吗?
CheckForIllegalCrossThreadCalls = false;
You're explicitly turning off checks for what you're doing wrong. 你明确地关闭了你做错的检查。 That suggests that you know you shouldn't be modifying the UI on a non-UI thread, but you're doing it anyway.
这表明你知道你不应该在非UI线程上修改UI,但无论如何你都是这样做的。 If you didn't have that line of code, you certainly would get exceptions when running in Visual Studio.
如果您没有这行代码,那么在Visual Studio中运行时肯定会遇到异常。
That's at least one of the problems. 这至少是其中一个问题。 Your
TimeOption
method is running on a non-UI thread, but it's modifying the UI. 您的
TimeOption
方法在非UI线程上运行,但它正在修改UI。 Just don't do that. 只是不要这样做。 There are various other options, including
BackgroundWorker
and Control.Invoke/BeginInvoke
. 还有其他各种选项,包括
BackgroundWorker
和Control.Invoke/BeginInvoke
。
Then... 然后...
TimeOption
and getTrigger
. TimeOption
和getTrigger
中都有紧密的循环。 Basically you're going to be pounding the database hard, forever. catch
blocks all over the place: catch {}
. catch
块: catch {}
。 That's basically claiming that whatever goes wrong, you're fine - you can just keep going and ignore it, without even logging what's happened. Thread.Abort
to kill your threads. Thread.Abort
来杀死你的线程。 Don't do that. bool
field indicating when you want to finish, and check it on each iteration of the loop. bool
字段非常简单,并在循环的每次迭代中检查它。 I suspect the problems are due to your inappropriate access to the UI from a different thread - but I can't say for sure. 我怀疑问题是由于您从不同的线程不恰当地访问UI - 但我不能肯定地说。 Fix all of the above and you'll have a much better codebase to then diagnose any problems which still remain.
修复上述所有内容,您将拥有更好的代码库,然后诊断仍然存在的任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.