ContextSwitchDeadlock Was Detected error in C#
The main thread of your program has been busy executing code for a minute. It is not taking care of its normal duties, pumping the message loop. That's illegal when you use COM servers in a worker thread: calls to their methods cannot be dispatched until your main thread goes idle again.
It should be readily visible, your UI should be dead as a door nail. Windows should have replaced your main window with a ghost that displays "Not Responding". Closing the window won't work, no click events have any effect.
Whatever your main thread is doing should be done by a worker thread instead. The BackgroundWorker
class is good for that, you'll find many usage help in the MSDN Library article for it. Use Debug + Break All, Debug + Windows + Threads if you have no idea what the main thread is doing.
One more possible cause: be sure to install service pack 1 if you are using the RTM version of VS2005.
Visual Studio: ContextSwitchDeadlock
The ContextSwitchDeadlock
doesn't necessarily mean your code has an issue, just that there is a potential. If you go to Debug > Exceptions
in the menu and expand the Managed Debugging Assistants
, you will find ContextSwitchDeadlock
is enabled.
If you disable this, VS will no longer warn you when items are taking a long time to process. In some cases you may validly have a long-running operation. It's also helpful if you are debugging and have stopped on a line while this is processing - you don't want it to complain before you've had a chance to dig into an issue.
ContextSwitchDeadlock was detected
You can ignore that exception (I've run into this before myself for long running methods).
- Hold
ctrl+alt+e
- Click
Find
- Type
ContextSwitchDeadlock
and press Enter - Uncheck
Thrown
in the table - Close the
Exceptions
configuration window by pressingOK
.NET - ContextSwitchDeadlock was detected
It is just a warning from a Managed Debugging Assistant (MDA). Your code is violating a pretty hard requirement for Single Threaded Apartment (STA) threads, they are not allowed to block for long periods. The warning is real enough, blocking the UI thread can easily cause deadlock. But the explanation in your case is simple, it just goes catatonic because it is busy computing, not because it actually blocked. The MDA can't tell the difference.
You can turn off the warning with Debug + Exceptions, open the Managed Debugging Assistants node and untick ContextSwitchDeadlock.
That still leaves the user with a window on her desktop that is dead to the world, not exactly a great user experience. And it can have side-effects, causing other programs to become unresponsive when they send messages to toplevel windows.
You do need to use threading to really solve this problem. Have a look at BackgroundWorker, it is well documented in the MSDN Library and many other places.
I am getting this error message `ContextSwitchDeadlock`
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 ofwhile 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.
Related Topics
How to Connect and Use a SQLite Database from C#
Using C# to Authenticate User Against Ldap
Creating Threads - Task.Factory.Startnew VS New Thread()
How to Dynamically Change Auto Complete Entries in a C# Combobox or Textbox
How to Setup Multiple Auth Schemes in ASP.NET Core 2.0
Find Image Format Using Bitmap Object in C#
Determine Client's Computer Name
Converting an Int[] to Byte[] in C#
How to Play a Sound in an ASP.NET Web Page
How to Specify Mapping Rule When Names of Properties Differ
In C# What Category Does the Colon ":" Fall Into, and What Does It Really Mean
What Is Applicationexception for in .Net
Explicitly Freeing Memory in C#
Why Can't I Use Interface with Explicit Operator