0

In my app, I have a code which refresh a GridView in an interval as below:

...
public void InitTimer()
    {
        timer1 = new System.Timers.Timer();
        timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
        timer1.Interval = 2000; 
        timer1.Start();
    }

private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        string[] port_List = SerialPort.GetPortNames();
        UpdateGridView(port_List);
        timer1.Start();
    }


public void UpdateGridView(string[] port_list)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
            Programmer_GV.Rows.Clear();
        foreach (string port in port_list)
        {
            hx_communator = new Hx_Communator(port);

            Programmer_GV.Rows.Add(port);
        }
        if (Programmer_GV.RowCount > 0)
        {
            int index = Programmer_GV.CurrentRow.Index;
            Programmer_GV.Rows[index].Selected = true;

        }
        else
        {
            //Programmer_GV.Rows.Add(40);
        }

    }
...

When I run my code I get different exceptions.

Sometimes I get Operation cannot be performed in this event handler.' on line Programmer_GV.Rows.Add(port);

Sometimes get Row index provided is out of range. Parameter name: rowIndex' on line Programmer_GV.Rows.Clear();

Sometimes get Specified argument was out of the range of valid values. on line Programmer_GV.Rows.Clear();

And sometimes get 'Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.' on line Programmer_GV.Rows.Clear();

I think my problem is on Programmer_GV.Rows.Clear(); line. I searched over the net and apply some other stack answers but none of them solved my problem.

Some of the links I search for my problem:

DataGridView Rows Error Index Out of Range c#

Index was out of range after deleting multiple rows from the datagridview? C#

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dd2d1db5-a9fe-4484-86d4-2202eb2c4ecd/datagridview-index-was-out-of-range-error?forum=vbgeneral

Emad Helmi
  • 652
  • 8
  • 25
  • 3
    You're using the wrong type of timer. You're using one that doesn't have thread affinity to access objects that are meant to be accessed from only the UI thread. – Damien_The_Unbeliever Jan 13 '20 at 13:02
  • 2
    The fact that you had to do `Control.CheckForIllegalCrossThreadCalls = false` should tell you everything you need to know here. You have a nasty race conflict caused by touching UI controls from threads that *aren't the UI thread*. That causes **very bad things**. Basically: don't do that – Marc Gravell Jan 13 '20 at 13:03
  • 1
    In fact, I suspect you're already aware of this, since you've already added the "I don't need a safety belt" line of `Control.CheckForIllegalCrossThreadCalls = false;` – Damien_The_Unbeliever Jan 13 '20 at 13:03
  • 1
    Usually getting an index out of range exception when calling the `Clear` method indicates a multi-threading issue: https://stackoverflow.com/questions/9393371/calling-listt-clear-causing-indexoutofrangeexception (this was the first result when searching for "indexoutofrangeexception when calling clear c#") – Rufus L Jan 13 '20 at 13:07
  • @Damien_The_Unbeliever Thank you. Your soloution helped me to Fix my problem – Emad Helmi Jan 14 '20 at 13:26

0 Answers0