Click here to Skip to main content
15,885,985 members
Articles / Containers / Virtual Machine

Observable property pattern, memory leaks, and weak delegates for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (42 votes)
4 Dec 200624 min read 217.6K   359   174  
This article is dedicated to the observable property design pattern, a very nice pattern used in the Microsoft .NET Framework, a possible memory leak problem with it, and gives a couple of ways to solve it.
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace Pvax.App.WinFormsIdleTest
{
	/// <summary>
	/// Description of StickyForm.
	/// </summary>
	public class StickyForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblIndicator;
		const int HogSize = 102400 * 1024; // 100 Kb chunks go to the large objects heap

		byte[] hog;

		const string phases = @"-/|\";

		int currentPhase = 0;

		public StickyForm()
		{
			hog = new byte[HogSize];
			for(int i = 0; i < hog.Length; i++)
				hog[i] = 5;
			InitializeComponent();
		}

		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.lblIndicator = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblIndicator
			// 
			this.lblIndicator.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lblIndicator.Location = new System.Drawing.Point(0, 0);
			this.lblIndicator.Name = "lblIndicator";
			this.lblIndicator.Size = new System.Drawing.Size(152, 110);
			this.lblIndicator.TabIndex = 1;
			this.lblIndicator.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// StickyForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(152, 110);
			this.Controls.Add(this.lblIndicator);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "StickyForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "OnIdle handler";
			this.ResumeLayout(false);
		}
		#endregion

		public void OnIdle(object sender, EventArgs e)
		{
			this.lblIndicator.Text = "" + phases[currentPhase];
			currentPhase++;
			if(phases.Length == currentPhase)
				currentPhase = 0;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'm a system administrator from Moscow, Russia. Programming is one of my hobbies. I presume I'm one of the first Russians who created a Web site dedicated to .Net known that time as NGWS. However, the Web page has been abandoned a long ago.

Comments and Discussions