Pregunta

I'm building a C# Gui. Included in it is a Refresh event which gets called every second or so to refresh the screen.

private void RefreshEverySecond_Tick(object o, EventArgs a) 
{

   if (Condition1) 
   {
      QuickStatusTextBox.Text = "Condition 1";
      QuickStatusTextBox.Font = new Font(QuickStatusTextBox.Font, FontStyle.Bold); 
   } 
   else 
   {
      QuickStatusTextBox.Text = "Condition 2";
      QuickStatusTextBox.Font = new Font(QuickStatusTextBox.Font, FontStyle.Regular); 
   } 

} 

In researching the way to do this, I've seen answers like this which encourage this behavior:

Easiest way to change font and font size with visual C#

BUT I've also seen a lot of chatter saying I should be using "Using" for IDisposable objects which I gather Font is.

When should I use "using" blocks in C#?

And examples of using with fonts: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

Question: What is the right way to change a text box from Bold to Regular at periodic intervals? Does my method violate any rules or risk a memory leak or contention because I'm not using "using", and is there a "proper" way using Using? Remember this updates every second... so I'm likely keeping the Garbage collector busy but what other side effects are going to bite me?

¿Fue útil?

Solución

A Font object in Winforms .NET actually encapsulates two things: a description of a typeface, and a handle to a GDI object which represents that typeface. One of those things represents a limited resource (the GDI handle) and the other does not.

While it might have been possible to have controls use the GDI handles of fonts which are used to set their properties, built-in controls do not do so. Instead, the line myControl.Font = myFont; will cause myControl to capture a description of the typeface encapsulated by myFont and make its own Font object for internal use. The control will ensure that its internal-use Font object gets disposed when either the control is disposed or its myControl.Font is set to a different font, but the control will neither dispose myFont nor care about when or whether it is disposed. Interestingly, it won't care if myFont had been disposed even before the statement above executed. Assuming nothing else has written it, reading myControl.Font will return a reference to myFont rather than the control's internal font object; if myFont has been disposed, then myControl.Font will return a reference to a font object which has been disposed (but could still be used to set other controls' Font properties).

If one wishes to most accurately ensure the prompt cleanup of GDI font resources, any font objects which will be used only as "templates" and won't be used for actual drawing may be disposed as soon as they are created. I don't know whether that should be recommended as a pattern, since I can't find any official documentation of controls' Font behavior, but it seems wasteful to have font objects holding GDI resources that are never going to be used. It's too bad controls' Font property doesn't use some sort of documented FontDescription class which would encapsulate just the typeface description but not the GDI handle, since such a design would make things a lot clearer.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top