title: Yet another C# Deadlock Debugging Question
body: <p>I have a multi-threaded application build in C# using VS2010 Professional. It's quite a large application and we've experienced the classing GUI cross-threading and deadlock issues before, but in the past month we've noticed the appears to lock up when left idle for around 20-30 minutes.</p>
  
  <p>The application is irresponsive and although it will repaint itself when other windows are dragged in front of the application and over it, the GUI still appears to be locked... interstingly (unlike if the GUI thread is being used for a considerable amount of time) the Close, Maximise and minimise buttons are also irresponsive and when clicked the little (Not Responding...) text is not displayed in the title of the application i.e. Windows still seems to think it's running fine.</p>
  
  <p>If I break/pause the application using the debugger, and view the threads that are running. There are 3 threads of our managed code that are running, and a few other worker threads whom the source code cannot be displayed for.</p>
  
  <p>The 3 threads that run are:</p>
  
  <ol>
  <li>The main/GUI thread</li>
  <li>A thread that loops indefinitely</li>
  <li>A thread that loops indefinitely</li>
  </ol>
  
  <p>If I step into threads 2 and 3, they appear to be looping correctly. They do not share locks (even with the main GUI thread) and they are not using the GUI thread at all. When stepping into the main/GUI thread however, it's broken on Application.Run...</p>
  
  <p>This problem screams deadlock to me, but what I don't understand is if it's deadlock, why can't I see the line of code the main/GUI thread is hanging on?</p>
  
  <p>Any help will be greatly appreciated! Let me know if you need more information...</p>
  
  <p>Cheers,</p>
  
  <p>Roo</p>
  
  <p><strong>-----------------------------------------------------SOLUTION--------------------------------------------------</strong></p>
  
  <p>Okay, so the problem is now solved. Thanks to everyone for their suggestions! Much appreciated! I've marked the answer that solved my initial problem of determining where on the main/UI thread the application hangs (I handn't turned off the "Enable Just My Code" option).</p>
  
  <p>The overall issue I was experiencing was indeed Deadlock, however. After obtaining the call-stack and popping the top half of it into Google I came across this which explains exactly what I was experiencing...</p>
  
  <p><a href="http://timl.net/" rel="nofollow noreferrer">http://timl.net/</a></p>
  
  <p>This references a lovely guide to debugging the issue...</p>
  
  <p><a href="http://www.aaronlerch.com/blog/2008/12/15/debugging-ui/" rel="nofollow noreferrer">http://www.aaronlerch.com/blog/2008/12/15/debugging-ui/</a></p>
  
  <p>This identified a control I was constructing off the GUI thread. I did know this, however, and was marshalling calls correctly, but what I didn't realise was that behind the scenes this Control was subscribing to an event or set of events that are triggered when e.g. a Windows session is unlocked or the screensaver exits. These calls are always made on the main/UI thread and were blocking when it saw the call was made on the incorrect thread. Kim explains in more detail here...</p>
  
  <p><a href="http://krgreenlee.blogspot.com/2007/09/onuserpreferencechanged-hang.html" rel="nofollow noreferrer">http://krgreenlee.blogspot.com/2007/09/onuserpreferencechanged-hang.html</a></p>
  
  <p>In the end I found an alternative solution which did not require this Control off the main/UI thread.</p>
  
  <p>That appears to have solved the problem and the application no longer hangs. I hope this helps anyone who's confronted by a similar problem.</p>
  
  <p>Thanks again to everyone on here who helped! (and indirectly, the delightful bloggers I've referenced above!)</p>
  
  <p>Roo</p>
  
  <p><strong>-----------------------------------------------------SOLUTION II--------------------------------------------------</strong></p>
  
  <p>Aren't threading issues delightful...you think you've solved it, and a month down the line it pops back up again. I still believe the solution above resolved an issue that would cause simillar behaviour, but we encountered the problem again.</p>
  
  <p>As we spent a while debugging this, I thought I'd update this question with our (hopefully) final solution:</p>
  
  <p>The problem appears to have been a bug in the Infragistics components in the WinForms 2010.1 release (no hot fixes). We had been running from around the time the freeze issue appeared (but had also added a bunch of other stuff too). After upgrading to WinForms 2010.3, we've yet to reproduce the issue (deja vu). See my question here for a bit more information: '<a href="https://stackoverflow.com/questions/4077822/net-4-0-and-the-dreaded-onuserpreferencechanged-hang">.NET 4.0 and the dreaded OnUserPreferenceChanged Hang</a>'. Hans has given a nice summary of the general issue.</p>
  
  <p>I hope this adds a little to the suggestions/information surrounding the nutorious OnUserPreferenceChanged Hang (or whatever you'd like to call it).</p>
  
  <p>Cheers,</p>
  
  <p>Roo</p>