I have a win Form and I want to read a text file and show it in list Box but there is problem when i try to load file with 1-3mb size after a few seconds its throw this exception:
Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0x149cf10 to COM context 0x149ce58 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.'
and here is my code:
private void Button7_Click(object sender, EventArgs e)
{
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog.FileName);
var emails = sr.ReadToEnd();
foreach (var items in emails)
{
aFlistBoxEmail.Items.Add(items);
}
}
}
also i use this solution but didnt help: enter link description here
You can try reading all lines at once :
private async void Button7_Click(object sender, EventArgs e)
{
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
aFlistBoxEmail.DataSource = File.ReadAllLines(openFileDialog.FileName);
}