简体   繁体   English

C#线程应用程序出错

[英]Error in C# threading application

I have created 4 projects to online examination time monitoring 我创建了4个在线考试时间监控项目

  1. ASP.NET application to examination ASP.NET应用程序来检查
  2. Class Library for Business Process and Data Access 业务流程和数据访问的类库
  3. Class Library to database execution 类库到数据库的执行
  4. Windows form application to monitor examination time Windows窗体应用程序监视检查时间

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 . 还有其他各种选项,包括BackgroundWorkerControl.Invoke/BeginInvoke

Then... 然后...

  • You've got tight loops in both TimeOption and getTrigger . 你在TimeOptiongetTrigger中都有紧密的循环。 Basically you're going to be pounding the database hard, forever. 基本上,你将永远在努力地敲击数据库。 That's a really bad idea. 这是一个非常糟糕的主意。 You should at least have a delay between iterations 你应该至少在迭代之间有一个延迟
  • You've got empty 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. 这基本上声称无论出现什么问题,你都没关系 - 你可以继续前进并忽略它,甚至不记录发生了什么。 Don't do that. 不要那样做。
  • You're using Thread.Abort to kill your threads. 你正在使用Thread.Abort来杀死你的线程。 Don't do that. 不要那样做。 In this case it would be pretty simple to use a volatile bool field indicating when you want to finish, and check it on each iteration of the loop. 在这种情况下,使用指示何时完成的volatile 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM