Skip to content

PipeBase<TMessage> implement the Dispose Pattern (#930) #1418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Microsoft.ML.Core/Environment/ConsoleEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,15 @@ public Channel(ConsoleEnvironment root, ChannelProviderBase parent, string short
Root._consoleWriter.ChannelStarted(this);
}

protected override void DisposeCore()
protected override void Dispose(bool disposing)
{
Watch.Stop();
Root._consoleWriter.ChannelDisposed(this);
base.DisposeCore();
if(disposing)
{
Watch.Stop();
Root._consoleWriter.ChannelDisposed(this);
}

base.Dispose(disposing);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ protected abstract class PipeBase<TMessage> : ChannelProviderBase, IPipe<TMessag

public readonly ChannelProviderBase Parent;

private bool _disposed;

protected PipeBase(ChannelProviderBase parent, string shortName,
Action<IMessageSource, TMessage> dispatch)
: base(shortName, parent.FullName, parent.Verbose)
Expand All @@ -192,10 +194,14 @@ protected PipeBase(ChannelProviderBase parent, string shortName,

public void Dispose()
{
DisposeCore();
if(!_disposed)
{
Dispose(true);
_disposed = true;
}
}

protected virtual void DisposeCore()
protected virtual void Dispose(bool disposing)
{
}

Expand Down
14 changes: 9 additions & 5 deletions src/Microsoft.ML.Data/Utilities/LocalEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public Channel(LocalEnvironment root, ChannelProviderBase parent, string shortNa
private void ChannelFinished()
=> Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel finished. Elapsed { 0:c }.", Watch.Elapsed));

protected override void DisposeCore()
protected override void Dispose(bool disposing)
{
ChannelFinished();
Watch.Stop();
if(disposing)
{
ChannelFinished();
Watch.Stop();

Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel disposed"));
base.DisposeCore();
Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel disposed"));
}

base.Dispose(disposing);
}
}

Expand Down