0

I am trying to read a "csv file", then save it's data into an employee record, then adding this record into a list, then adding the list into a data table and finally display the datatables content inside a datagridview. I can't seem to figure out if I have an endless loop running or what the problem may be. The program runs for 1 minute then throws the error ContextSwitchDeadlock.

private void searchButton_Click(object sender, EventArgs e)
{
    string headerLine = reader.ReadLine();
    openFileDialog1.ShowDialog();
    searchValue.Text = openFileDialog1.FileName;

    using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
    {        
        var line = reader.ReadLine();
        var value = line.Split(',');

        while(!reader.EndOfStream)
        {
            List<Employee> employeeList = new List<Employee>();
            var newEmployee = new Employee();

            newEmployee.firstName = value[0];
            newEmployee.lastName = value[1];
            newEmployee.address = value[2];
            newEmployee.age = value[3];
            newEmployee.monthlyGrossIncome = value[4];
            newEmployee.departmentId = value[5];
            newEmployee.developerType = value[6];
            newEmployee.taxType = value[7];

        }
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add(headerLine);
        employeeDataGridView.DataSource = dataTable;
    }
}
Sebastian
  • 2,874
  • 25
  • 31

1 Answers1

0

Firstly you have to see what are you doing, and what you want to do actually.

  • var line = reader.ReadLine(); var value = line.Split(',');

    These two lines should be inside the while loop. So, that you are actually reading all contents of csv file line by line. Right now its just iterating only one line.

  • List<Employee> employeeList = new List<Employee>();

    Instead of making one list for all the employees, you are creating a new list on every iteration of while loop.

  • You are not inserting the employee object in the list that you have created.

Well, I am not 100% sure that fixing all this will solve the problem on your system but I have created a dummy project like this one on my machine and it is working.

Correct Code:

private void searchButton_Click(object sender, EventArgs e)
{
    string headerLine = reader.ReadLine();
    openFileDialog1.ShowDialog();
    searchValue.Text = openFileDialog1.FileName;

    using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
    {        
        List<Employee> employeeList = new List<Employee>();
        while(!reader.EndOfStream)
        {
            var value = reader.ReadLine().Split(',');
            var newEmployee = new Employee
            {
                firstName = value[0],
                lastName = value[1],
                address = value[2],
                age = value[3],
                monthlyGrossIncome = value[4],
                departmentId = value[5],
                developerType = value[6],
                taxType = value[7]
            };
            employeeList.Insert(newEmployee);   
        }
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add(headerLine);
        employeeDataGridView.DataSource = dataTable;
    }
}

Now, coming to the ContexSwitchDeadlock, it is a Debugging Assistant in Visual Studio and is a tool provided by debugger while debugging an application. I am not cocksure about the conditions it checks before raising this. But note that its not an exception and it's just visual studio alerting you that it is waiting for 60 seconds. Do let me know if this solves the issue.

Sebastian
  • 2,874
  • 25
  • 31
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • The deadlock is the while-loop, if you do not reading the next line endofstream is false in every loop. – Sebastian Aug 15 '19 at 08:46
  • This definitely solved my issue I really appreciate it! – gtownflako18 Aug 16 '19 at 03:13
  • @gtownflako18, New users must take this to account, that if they find the answer correct and it resolves their issue, then kindly mark that as answer. so that if someone have the same issue in future they can get a handy help without asking a new question. That's how things work here . Welcome to SO :) – Zain Arshad Aug 16 '19 at 10:28
  • 1
    @ZainArshad not sure how to do that but I clicked the check on top hopefully that's how you do it. Thank You! – gtownflako18 Aug 17 '19 at 23:51