Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does changing Form.Text to non-empty cause a program to display "(Not Responding)"?
    text
    copied!<p>I have come across some behavior that I have no idea how to explain. When I repeatedly draw to a <code>System.Windows.Forms.Form</code> without being careful not to hang the thread, after several seconds the drawing output freezes and I see the dreaded <strong>(Not Responding)</strong> text in the title: <img src="https://i.stack.imgur.com/qHsZG.png" alt="Not Responding"></p> <p>This screenshot isn't mid-draw; the image it was drawing is completely red at the end of the program, but the drawing has stopped about halfway through. <strong>The weird thing is if I skip setting the <code>Form.Text</code> property before drawing, it doesn't freeze.</strong> Can anybody explain why?</p> <p>I was using a loop to draw the progress of some multithreaded graphical fill algorithms, but was seeing the screen output freeze. Since I was drawing and then calling <code>Thread.Sleep()</code> in a loop, I should have expected this behavior (<a href="https://stackoverflow.com/questions/4919058/form-not-responding-when-any-other-operation-performed-in-c-sharp">other posts</a> point out I probably should have used <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow noreferrer"><code>BackgroundWorker</code></a>, but just using <code>Application.DoEvents()</code> stopped the freezing for me). </p> <p>But what really surprised me when chopping down the code to find my bug was that when I removed the call to change <code>form.Text</code>, it doesn't freeze anymore! It isn't calling the setter that does anything either; assigning to an empty string (<code>form.Text = "";</code>) also causes it not to freeze. So, for some reason when <code>Text</code> is empty Windows doesn't care the program isn't responding, and will happily keep showing the drawing progress! <strong>What is going on? Does Windows treat programs with empty titles differently?</strong></p> <p>I reduced the code down to the bare minimum of what was required to see the freeze. While the program is running the form image will get filled with red by the loop. About halfway through filling it just stops, even though the loop continues running! You see that after it finishes it handles events and draws one more time to show the whole image is red, then stays around for two more seconds.</p> <p>(If you run with debugging (F5) it doesn't freeze; you must run without debugging (Ctrl-F5). That made figuring all this out all that much harder!)</p> <pre><code>using System.Drawing; using System.Threading; using System.Windows.Forms; // Include resources System.Drawing, System.Windows.Forms in VS before compiling class Program { static void Main(string[] args) { Bitmap image = new Bitmap(400, 20); Form form = new Form(); form.Text = "some title"; // Comment this out to avoid the freeze form.SetBounds(30, 30, image.Width + 16, image.Height + 38); form.Show(); Graphics graphics = Graphics.FromHwnd(form.Handle); for (int x = 0; x &lt; 400; ++x) for (int y = 0; y &lt; 20; ++y) { image.SetPixel(x, y, Color.Red); graphics.DrawImage(image, 0, 0); Thread.Sleep(1); // Calling this would prevent the freeze: //Application.DoEvents(); } Application.DoEvents(); graphics.DrawImage(image, 0, 0); Thread.Sleep(2000); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload