Search code examples
c#contextswitchdeadlock

I am getting this error message `ContextSwitchDeadlock`


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;
    }
}

Solution

  • 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.