Commit b91934e3 authored by Martin Finkel's avatar Martin Finkel

Add MediaDiscoverer IDisposable implementation

(cherry picked from commit 2e22ef80)
parent 4ac9607a
using System.Linq;
using System;
using System.Linq;
using LibVLCSharp.Shared;
using NUnit.Framework;
......@@ -18,5 +19,20 @@ namespace LibVLCSharp.Tests
md.Stop();
Assert.False(md.IsRunning);
}
[Test]
public void DisposeMediaDiscoverer()
{
var libVLC = new LibVLC();
var mds = libVLC.MediaDiscoverers(MediaDiscoverer.Category.Lan);
var md = new MediaDiscoverer(libVLC, mds.First().Name);
Assert.True(md.Start());
Assert.True(md.IsRunning);
Assert.NotNull(md.MediaList);
md.Dispose();
Assert.IsNull(md.MediaList);
Assert.False(md.IsRunning);
Assert.AreEqual(IntPtr.Zero, md.NativeReference);
}
}
}
\ No newline at end of file
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace LibVLCSharp.Shared
{
......@@ -129,7 +128,9 @@ namespace LibVLCSharp.Shared
/// <summary>
/// Query if media service discover object is running.
/// </summary>
public bool IsRunning => Native.LibVLCMediaDiscovererIsRunning(NativeReference) != 0;
public bool IsRunning => NativeReference != IntPtr.Zero ?
Native.LibVLCMediaDiscovererIsRunning(NativeReference) != 0
: false;
/// <summary>
/// The MediaList attached to this MediaDiscoverer
......@@ -140,6 +141,8 @@ namespace LibVLCSharp.Shared
{
if (_mediaList == null)
{
if (IsDisposed || NativeReference == IntPtr.Zero) return null;
var ptr = Native.LibVLCMediaDiscovererMediaList(NativeReference);
if (ptr == IntPtr.Zero) return null;
_mediaList = new MediaList(ptr);
......@@ -163,5 +166,32 @@ namespace LibVLCSharp.Shared
}
#endregion
protected override void Dispose(bool disposing)
{
if (IsDisposed || NativeReference == IntPtr.Zero)
return;
if(disposing)
{
if(_mediaList != null)
{
_mediaList.Dispose();
_mediaList = null;
}
if(IsRunning)
{
Stop();
}
}
base.Dispose(disposing);
}
~MediaDiscoverer()
{
Dispose(false);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment