Skip to content

Commit

Permalink
Let the AutomaticUpdater be disposable.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattoday committed Feb 5, 2012
1 parent f60c604 commit c897c2f
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 6 deletions.
32 changes: 31 additions & 1 deletion Control/AutomaticUpdater.cs
Expand Up @@ -23,7 +23,7 @@ namespace wyDay.Controls
[ToolboxBitmapAttribute(typeof(AutomaticUpdater), "update-notify.png")]
public class AutomaticUpdater : ContainerControl, ISupportInitialize
{
readonly AutomaticUpdaterBackend auBackend = new AutomaticUpdaterBackend(true);
AutomaticUpdaterBackend auBackend = new AutomaticUpdaterBackend(true);

Form ownerForm;

Expand Down Expand Up @@ -419,6 +419,36 @@ protected override void OnSizeChanged(EventArgs e)

#endregion

#region Dispose

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
// Not already disposed ?
if (disposing)
{
// Not already disposed ?
if (auBackend != null)
{
auBackend.Dispose(); // Dispose it
auBackend = null; // Its now inaccessible
}

if (backBrush != null)
{
backBrush.Dispose(); // Dispose it
backBrush = null; // Its now inaccessible
}
}

base.Dispose(disposing);
}

#endregion

//Methods and such

/// <summary>
Expand Down
60 changes: 58 additions & 2 deletions Control/AutomaticUpdaterBackend.cs
Expand Up @@ -8,11 +8,11 @@ namespace wyDay.Controls
/// <summary>
/// Backend for the AutomaticUpdater control.
/// </summary>
public class AutomaticUpdaterBackend
public class AutomaticUpdaterBackend : IDisposable
{
AutoUpdaterInfo AutoUpdaterInfo;

readonly UpdateHelper updateHelper = new UpdateHelper();
UpdateHelper updateHelper = new UpdateHelper();

UpdateStepOn m_UpdateStepOn;

Expand Down Expand Up @@ -256,6 +256,62 @@ public string wyUpdateLocation

#endregion

#region Dispose

/// <summary>
/// Indicates whether this instance is disposed.
/// </summary>
private bool isDisposed;

/// <summary>
/// Finalizes an instance of the <see cref="AutomaticUpdaterBackend"/> class.
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="AutomaticUpdaterBackend"/> is reclaimed by garbage collection.
/// </summary>
~AutomaticUpdaterBackend()
{
Dispose(false);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">Result: <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
// Not already disposed ?
if (disposing)
{
// dispose managed resources
// Not already disposed ?
if (updateHelper != null)
{
updateHelper.Dispose(); // Dispose it
updateHelper = null; // Its now inaccessible
}
}

// free unmanaged resources
// Set large fields to null.

// Instance is disposed
isDisposed = true;
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion

/// <summary>
/// Represents an AutomaticUpdater control.
/// </summary>
Expand Down
64 changes: 63 additions & 1 deletion Control/PipeClient.cs
Expand Up @@ -9,7 +9,7 @@ namespace wyDay.Controls
/// <summary>
/// Allow pipe communication between a server and a client
/// </summary>
public class PipeClient
public class PipeClient : IDisposable
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
static extern SafeFileHandle CreateFile(
Expand Down Expand Up @@ -59,6 +59,68 @@ public class PipeClient
/// </summary>
public string PipeName { get; private set; }

#region Dispose

/// <summary>
/// Indicates whether this instance is disposed.
/// </summary>
bool isDisposed;

/// <summary>
/// Finalizes an instance of the <see cref="PipeClient"/> class.
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="PipeClient"/> is reclaimed by garbage collection.
/// </summary>
~PipeClient()
{
Dispose(false);
}

/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
/// <param name="disposing">Result: <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
// Not already disposed ?
if (disposing)
{
// dispose managed resources
// Not already disposed ?
if (stream != null)
{
Connected = false;
stream.Dispose(); // Dispose it
stream = null; // Its now inaccessible
}

if (handle != null)
{
Connected = false;
handle.Dispose();
handle = null;
}
}

// free unmanaged resources
// Set large fields to null.

// Instance is disposed
isDisposed = true;
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion

/// <summary>
/// Connects to the server with a pipename.
/// </summary>
Expand Down
61 changes: 60 additions & 1 deletion Control/UpdateHelper.cs
Expand Up @@ -11,7 +11,7 @@

namespace wyDay.Controls
{
internal class UpdateHelper
internal class UpdateHelper : IDisposable
{
// Constants
const int MaxSendRetries = 20;
Expand Down Expand Up @@ -68,6 +68,65 @@ public string wyUpdateLocation
readonly List<UpdateHelperData> receivedBuffer = new List<UpdateHelperData>();
public bool BufferResponse;

#region Dispose

/// <summary>Indicates whether this instance is disposed.</summary>
private bool isDisposed;

/// <summary>
/// Finalizes an instance of the <see cref="UpdateHelper"/> class.
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="UpdateHelper"/> is reclaimed by garbage collection.
/// </summary>
~UpdateHelper()
{
Dispose(false);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">Result: <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
// Not already disposed ?
if (disposing)
{
// dispose managed resources
// Not already disposed ?
if (pipeClient != null)
{
pipeClient.Dispose(); // Dispose it
pipeClient = null; // Its now inaccessible
}
if (bw != null)
{
bw.Dispose(); // Dispose it
bw = null; // Its now inaccessible
}
}

// free unmanaged resources
// Set large fields to null.

// Instance is disposed
isDisposed = true;
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion

public void FlushResponses()
{
lock (receivedBuffer)
Expand Down
1 change: 0 additions & 1 deletion ControlWPF/AutomaticUpdater.cs
Expand Up @@ -404,7 +404,6 @@ public Brush Foreground

#endregion


public AutomaticUpdater()
{
Animate = true;
Expand Down
1 change: 1 addition & 0 deletions ControlWPF/AutomaticUpdaterWPF.csproj
Expand Up @@ -89,6 +89,7 @@
</Compile>
<Compile Include="..\Control\MLLabel.cs">
<Link>MLLabel.cs</Link>
<SubType>Component</SubType>
</Compile>
<Compile Include="..\Control\PipeClient.cs">
<Link>PipeClient.cs</Link>
Expand Down
1 change: 1 addition & 0 deletions ControlWPF4/AutomaticUpdaterWPF4.csproj
Expand Up @@ -98,6 +98,7 @@
</Compile>
<Compile Include="..\Control\MLLabel.cs">
<Link>MLLabel.cs</Link>
<SubType>Component</SubType>
</Compile>
<Compile Include="..\Control\PipeClient.cs">
<Link>PipeClient.cs</Link>
Expand Down

0 comments on commit c897c2f

Please sign in to comment.