ContextSwitchDeadlock

B

Benjamin Vigneaux

Hello!!

Well... this code works perfectly... what it does:
It navigates a folder tree of N-Levels and it moves all the files from each
folder to the root folder, then deletes the empty folders.

protected void MoveStuff(DirectoryInfo RootFolder)
{
if (RootFolder.FullName != textBoxROOTpath.Text)
{
foreach (FileInfo NextFile in RootFolder.GetFiles())
{
NextFile.CopyTo(textBoxROOTpath.Text+"\\"+NextFile.Name,true);
NextFile.Delete();
progressBar.PerformStep();
}
}

foreach (DirectoryInfo NextFolder in
RootFolder.GetDirectories())
{
MoveStuff(NextFolder);
}
foreach (DirectoryInfo EmptyFolder in
RootFolder.GetDirectories())
{
EmptyFolder.Delete();
}
}

My problem is, that it gets ugly with large files because of memory usage, I
guess.... the program becomes nonresponsive for a while, even though, most
of the cases, it will become responsive again... in one od the hungups I
came across this...

***
ContextSwitchDeadlock was detected:

The CLR has been unable to transition from COM context 0x1f6050 to COM
context 0x1f61c0 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.
***

I kind of understand what it says... but I do not know how to implement a
solution...

Any ideas?

Benjamin.
 
J

Jon Skeet [C# MVP]

I kind of understand what it says... but I do not know how to implement a
solution...

Any ideas?

My guess is that you're doing this in the UI thread. Don't - use a
BackgroundWorker instead.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top