Make SpaceNav disposable, remove lock

This commit is contained in:
Thibaud Gasser 2018-05-30 19:36:43 +02:00
parent 6e3b39a78f
commit 50402abe53

View File

@ -3,13 +3,12 @@ using System.Runtime.InteropServices;
namespace spnavwrapper
{
public sealed class SpaceNav
public sealed class SpaceNav : IDisposable
{
double _sensitivity = 1.0;
int _threshold = 5;
static SpaceNav instance = null;
static readonly object padlock = new object();
static SpaceNav instance;
const int SPNAV_EVENT_MOTION = 1;
const int SPNAV_EVENT_BUTTON = 2;
@ -61,17 +60,9 @@ namespace spnavwrapper
spnav_open(SPNAV_VENDOR_ID, SPNAV_PRODUCT_ID);
}
~SpaceNav()
{
// TODO : handle retcode
spnav_close();
}
public static SpaceNav Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
@ -80,20 +71,14 @@ namespace spnavwrapper
return instance;
}
}
}
public void CloseDevice()
{
lock (padlock)
{
spnav_close();
instance = null;
}
}
public SpaceNavEvent WaitEvent()
{
lock (padlock)
{
SpNavEvent sev = new SpNavEvent();
spnav_wait_event(ref sev);
@ -108,11 +93,8 @@ namespace spnavwrapper
return null;
}
}
}
public SpaceNavEvent WaitEvent(int milliseconds)
{
lock (padlock)
{
SpNavEvent sev = new SpNavEvent();
int ev_type = spnav_wait_event_timeout(ref sev, milliseconds);
@ -127,7 +109,6 @@ namespace spnavwrapper
return null;
}
}
}
public double Sensitivity
{
@ -137,15 +118,12 @@ namespace spnavwrapper
}
set
{
lock (padlock)
{
// TODO : handle retcode
spnav_sensitivity(value);
_sensitivity = value;
}
}
}
public int Threshold
{
@ -154,14 +132,38 @@ namespace spnavwrapper
return _threshold;
}
set
{
lock (padlock)
{
// TODO : handle retcode
spnav_deadzone(value);
_threshold = value;
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
void Dispose(bool disposing)
{
if (!disposedValue)
{
// Free unmanaged resources
spnav_close();
disposedValue = true;
}
}
~SpaceNav()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}