ContextSwitchDeadlock

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Vigneaux

    #1

    ContextSwitchDeadlock

    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(Direc toryInfo RootFolder)
    {
    if (RootFolder.Ful lName != textBoxROOTpath .Text)
    {
    foreach (FileInfo NextFile in RootFolder.GetF iles())
    {
    NextFile.CopyTo (textBoxROOTpat h.Text+"\\"+Nex tFile.Name,true );
    NextFile.Delete ();
    progressBar.Per formStep();
    }
    }

    foreach (DirectoryInfo NextFolder in
    RootFolder.GetD irectories())
    {
    MoveStuff(NextF older);
    }
    foreach (DirectoryInfo EmptyFolder in
    RootFolder.GetD irectories())
    {
    EmptyFolder.Del ete();
    }
    }

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

    ***
    ContextSwitchDe adlock 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 CoWaitForMultip leHandles) 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.

  • Jon Skeet [C# MVP]

    #2
    Re: ContextSwitchDe adlock

    On Jun 17, 3:42 pm, "Benjamin Vigneaux" <b...@mi.clwrot e:

    <snip>
    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
    BackgroundWorke r instead.

    Jon

    Comment

    • Peter Duniho

      #3
      Re: ContextSwitchDe adlock

      On Tue, 17 Jun 2008 07:42:34 -0700, Benjamin Vigneaux <benj@mi.clwrot e:
      [...]
      I kind of understand what it says... but I do not know how to implement
      a solution...
      Without a concise-but-complete code sample, it's impossible top say what
      your problem is for sure.

      However, it sure sounds like you are trying to run that code on your main
      GUI thread. I.e. in direct response to some UI event, like a button click
      or menu selection or something like that.

      Don't do that. Learn about BackgroundWorke r and other techniques for
      putting long-running activities on threads other than your main GUI
      thread. That will allow the main GUI thread to continue to be able to
      receive and process messages, and your program will remain responsive.

      Pete

      Comment

      • Benjamin Vigneaux

        #4
        Re: ContextSwitchDe adlock

        Jon, Peter, thnx for your help..i'll look into it...

        Comment

        Working...