none
.NET/C# program going to NOT RESPONDING RRS feed

  • Question

  • i have started my program in debug mode and its going NOT RESPONDING  but there are no any EXCEPTION ,. why i can detect an error??or where program going NOT RESPONDING 
    /??program using WCF service , SQL Compact DB, and connected with COM- serial port, program going not responding when program sending  requists to PORT
    this is my repeatable code, that sends requists to COM port

     
      bool timerstarted = false;
            private void timer_Tick(object sender, EventArgs e)
            {
                try
                {
                    labeltimer.Text = DateTime.Now.ToString();
                    if (timerstarted == true)
                    {
                        return;
                    }
                    timerstarted = true;
                    _CARDCODE = 0;
                    string error = "";
                    bool hasbassed = false;
                    if (this._Close == true)
                    {
                        this.Close();
                    }
    
                    if (serialPort1.IsOpen)
                    {
                        if (i < 8)
                        {
                            i++;
                        }
                        else
                        {
                            i = 0;
                        }
                        to[0] = (byte)(128 + i);
                        try
                        {
                            serialPort1.Write(to, 0, 1);
                        }
                        catch (System.Exception ex)
                        {
    throw;
                            ListViewItem Lvi = new ListViewItem((++rowcount).ToString());
                            Lvi.SubItems.Add("");
                            Lvi.SubItems.Add("");
                            Lvi.SubItems.Add("Disconnected");
                            Lvi.SubItems.Add("");
                            listView.Items.Add(Lvi);
                            StartPort();
                        }
    
    
                        if (progressBar.Value >= 1500)
                            progressBar.Value = 0;
                        else
                            progressBar.Value += 10;
    
                        if (serialPort1.BytesToRead != 0)
                        {
    
                            byte[] data = new byte[serialPort1.BytesToRead];
                            from = new byte[3];
                            try
                            {
                                serialPort1.Read(data, 0, data.Length);
                            }
                            catch (System.Exception ex)
                            {
    throw;
                                ListViewItem LVi = new ListViewItem((++rowcount).ToString());
                                LVi.SubItems.Add("");
                                LVi.SubItems.Add("");
                                LVi.SubItems.Add("Disconnected");
                                LVi.SubItems.Add("");
                                listView.Items.Add(LVi);
                                StartPort();
                            } 
                            // data.Length == 4 by Button or HassPassed
                            if (data.Length == 4)
                            {
                                if (data[1] > 2)
                                    DBS.AddButtonPass(data[0], data[1] / 4);
                                else
                                {
                                    DBS.EditPass((int)data[0]);
                                    timerstarted = false; 
                                    return; 
                                }
    
                            }
                            else if ((data[data.Length - 1] != 255) || (data.Length < 4))
                            {
                                from[0] = data[0];
                                from[1] = 0;
                                from[2] = 0;
                                serialPort1.Write(from, 0, 3);
                                timerstarted = false;
                                return;
                            }
    
                            //RegistoringSubscriber_ID Add Subscriber Card
                            else if ((RegistoringSubscriber_ID > 0) && (RegisterCheckpoint != 0) && (data[0] == RegisterCheckpoint))
                            {
                                Registoring = false;
                                if (RegistorNewCard(Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString())) > 0)
                                    FormBringPC.AnswerType = 1;
                                else
                                    FormBringPC.AnswerType = 2;
                                RegistoringSubscriber_ID = 0;
    
                                error = "register card";
                            }
                            // RegistoringSubscriber_ID < 0 Add Bonus Card
                            else if ((RegistoringSubscriber_ID < 0) && (RegisterCheckpoint != 0) && (data[0] == RegisterCheckpoint))
                            {
                                Registoring = false;
                                if (RegistorBonusCard(Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString())) > 0)
                                    FormBringPC.AnswerType = 1;
                                else
                                    FormBringPC.AnswerType = 2;
                                RegistoringSubscriber_ID = 0;
    
                                error = "register Bonus card";
                            }
                            else if ((List_Checkpoint_ID.Contains(data[0]) && ((data[1] == 130) || (data[1] == 129))))
                            {
                               //     DBS.EditPass((int)data[0]);
                            }
                            else if ((List_Checkpoint_ID.Contains(data[0]) && (data.Length == 7)))
                            {
                                _CARDCODE = Convert.ToInt32(data[2].ToString() + data[3].ToString() + data[4].ToString());
                                GlobalTypes.InvalidPass pass = DBS.CheckPassStatus(_CARDCODE, (int)data[0], (int)data[1]);
                                if (pass == GlobalTypes.InvalidPass.Valid)
                                {
                                    from[0] = data[0];
                                    from[1] = 1;
                                    from[2] = 1;
                                    serialPort1.Write(from, 0, 3);
                                    LBTitle.Text = DateTime.Now.ToLongTimeString();
                                    hasbassed = true;
                                }
                                else
                                {
                                    from[0] = data[0];
                                    from[1] = 0;
                                    from[2] = 0;
                                    serialPort1.Write(from, 0, 3);
                                    switch (pass)
                                    {
                                       //some messegase
                                    }
                                }
                            }
                            else if ((List_Checkpoint_ID.Contains(data[0]) && (data.Length > 0)))
                            {
                                try
                                {
                                    byte[] ticket = new byte[data.Length - 6];
    
                                    for (int ii = 0; ii < data.Length - 6; ii++)
                                    {
                                        ticket[ii] = data[ii + 2];
                                    }
    
                                    _CARDCODE = Convert.ToInt32(Encoding.ASCII.GetString(ticket.ToArray()));
                                    GlobalTypes.HasPass pass = DBS.CheckTicketPassStatus(_CARDCODE, (int)data[0], (int)data[1]);
                                    if (pass == GlobalTypes.HasPass.TruePass)
                                    {
                                        from[0] = data[0];
                                        from[1] = 1;
                                        from[2] = 1;
                                        serialPort1.Write(from, 0, 3);
                                        LBTitle.Text = DateTime.Now.ToLongTimeString();
                                        hasbassed = true;
                                    }
                                    else
                                    {
                                        from[0] = data[0];
                                        from[1] = 0;
                                        from[2] = 0;
                                        serialPort1.Write(from, 0, 3);
                                        switch (pass)
                                        {
    //some messegaes
                                        }
                                    }
                                }
                                catch { throw; timerstarted = false; return; }
                            }
                            if (rowcount >= 10)
                            {
                                rowcount = 0;
                                listView.Items.Clear();
                            }
                            ListViewItem lvi = new ListViewItem((++rowcount).ToString());
                            lvi.SubItems.Add(data[0].ToString());
                            if (data.Length == 4)
                                hasbassed = true;
                            if (((data[1] == 2) || (data[1] == 8)) && ((data.Length == 4) || (Dictionary_Chekpoint[data[0]].CheckPointType == 2)))
                            {
                                lvi.SubItems.Add("Ելք");
                            }
                            else
                            {
                                lvi.SubItems.Add("Մուտք");
                            }
                            if (data.Length != 4)
                                lvi.SubItems.Add(_CARDCODE.ToString());
                            else
                                lvi.SubItems.Add("Հերթապահ");
                            lvi.SubItems.Add(error);
                            if (hasbassed == true)
                            {
                                lvi.BackColor = Color.LightGreen;
                            }
                            else
                            {
                                lvi.BackColor = Color.LightPink;
                            }
                            listView.Items.Add(lvi);
                        }
                    }
                    else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    timerstarted = false;
                }
                catch 
                { throw;
                    timerstarted = false;
                    return; 
                }
           }





     




     
    Thursday, June 25, 2015 7:53 AM

Answers

  • You're using SerialPort in the main thread. Some operations on the SerialPort are blocking. Thus - I guess - that's your issue.

    Cause your method mixes UI code with logic, I would decouple this. Place your SerialPort code into a separate class. Use events and Invoke() and InvokeRequired() to update your UI from there.

    Thursday, June 25, 2015 9:27 AM

All replies

  • Please post readable code...

    What does NOT RESPONDING mean? Windows marks your window with (Not Reponding)? This means that you're program is so busy, that it even cannot process basic Windows messages.

    Thursday, June 25, 2015 8:16 AM
  • program cannot do anything, just stoped,
    Thursday, June 25, 2015 8:31 AM
  • You're using SerialPort in the main thread. Some operations on the SerialPort are blocking. Thus - I guess - that's your issue.

    Cause your method mixes UI code with logic, I would decouple this. Place your SerialPort code into a separate class. Use events and Invoke() and InvokeRequired() to update your UI from there.

    Thursday, June 25, 2015 9:27 AM
  • thanks i will try it 
    Thursday, June 25, 2015 9:45 AM