当前位置: 代码迷 >> C# >> 急求教小弟我写的程序运行一段时间后,程序未响应,如果您继续等待或许响应
  详细解决方案

急求教小弟我写的程序运行一段时间后,程序未响应,如果您继续等待或许响应

热度:30   发布时间:2016-05-05 03:57:00.0
急!求教我写的程序运行一段时间后,程序未响应,如果您继续等待或许响应。
源代码如下:
        public WorkForm()
        {
            InitializeComponent();
          }
        System.Threading.Timer t = null;
        private void button2_Click(object sender, EventArgs e)
        {
           

            //MessageBox.Show(list[comboBox2.SelectedIndex]);
            if (button2.Text == "开始扫描")
            {
                try
                {
                    serialPort1.PortName = SearchExcelCom();
                    serialPort1.Open();
                    //使用Timer循环       
                    t = new System.Threading.Timer(new System.Threading.TimerCallback(timerRead), null, 0, 4000);

                    comboBox1.Enabled = false;
                    comboBox2.Enabled = false;
                    listView1.Enabled = false;
                    button2.Text = "停止扫描";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("C+" + ex.Message);
                }
                
                
            }
            else
            {
                DialogResult dr = MessageBox.Show("停止扫描将会清空列表,是否继续?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    try
                    {
                        t.Dispose();

                        serialPort1.Close();
                        comboBox1.Enabled = true;
                        comboBox2.Enabled = true;
                        button2.Text = "开始扫描";
                        listView1.Enabled = true;

                        DataTable dt = new DataTable("WorkList");

                        DataColumn dc4 = new DataColumn("编号", Type.GetType("System.String"));
                        DataColumn dc1 = new DataColumn("品号", Type.GetType("System.String"));
                        DataColumn dc2 = new DataColumn("名称", Type.GetType("System.String"));
                        DataColumn dc3 = new DataColumn("打印时间", Type.GetType("System.String"));

                        dt.Columns.Add(dc4);
                        dt.Columns.Add(dc1);
                        dt.Columns.Add(dc2);
                        dt.Columns.Add(dc3);

                        dataGridView1.DataSource = dt;

                        //label5.ForeColor = Color.Blue;
                        label8.Text = "0";
                    }
                    catch (Exception ec)
                    {
                        MessageBox.Show("D+" + ec.Message);
                    }
                    

                }
            }
           
            
            
        }
        
        void timerRead(object obj)
        {

            try
            {
                byte[] byteBuffer = new byte[] { 0x02, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x03, 0x35, 0x35 };

                serialPort1.Write(byteBuffer, 0, byteBuffer.Length);
            }
            catch (Exception mes)
            {
                MessageBox.Show("B+" + mes.Message);
                return;
            }
            
        }
        void b_LabelPrinted(object sender, EventArgs e)
        {

        }
        private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count - 1 > 0)
                {
                    DateTime dtime = Convert.ToDateTime(dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[3].Value.ToString());

                    TimeSpan ts = DateTime.Now.Subtract(dtime);

                    if (ts.Seconds <= 5)
                    {
                        return;
                    }
                }
                byte[] rs = new byte[serialPort1.BytesToRead];
                serialPort1.Read(rs, 0, rs.Length);

                string qsss = "";
                for (int i = 0; i < rs.Length; i++)
                {
                    qsss += rs[i].ToString("X2") + " ";
                }

                rs = null;

                //serialPort1.Close();
                if (qsss.Equals("02 30 31 03 36 34 "))//!string.IsNullOrEmpty(rs[0].ToString())
                {

                    string Count = "0";
                    if (label9.Text.Equals("晚班"))
                    {
                        Count = "1";
                    }

                    DataTable dt = (DataTable)dataGridView1.DataSource;

                    DataRow dr = dt.NewRow();

                    dr["编号"] = dataGridView1.Rows.Count.ToString();
                    dr["品号"] = comboBox3.Text;
                    dr["名称"] = comboBox2.Text;
                    dr["打印时间"] = DateTime.Now.ToString();
                    dt.Rows.Add(dr);
                    dataGridView1.DataSource = dt;

                    this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1;
                    label8.Text = (dataGridView1.Rows.Count - 1).ToString();
                    //打印条码                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("A+" + ex.Message);
            }
            
        }

        
------解决思路----------------------
太高端了……不懂,加日志什么的看下