Skip to content
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

IBlink has Dispose() but doesn't implement IDisposable #12

Open
ImaginaryDevelopment opened this issue Oct 25, 2018 · 2 comments
Open
Assignees

Comments

@ImaginaryDevelopment
Copy link

https://github.com/SleddogSoftwareDevelopment/blink1/blob/master/src/Sleddog.Blink1/IBlink1.cs

Sleddog.Blink1.Blink1Connector.Scan() used to return a type that implements IDisposable and the method is carried forward but it doesn't actually implement it.

@ImaginaryDevelopment
Copy link
Author

c# sample work around

//references nuget packages hidlibrary and NAudio
using NAudio;
using System.Reactive.Linq;
using Sleddog.Blink1;
using System.Reactive;
using NAudio.Wave;
void Main()
{
	// by imaginarydevelopment.blogspot.com
	// twitter @maslowjax
	Listen(() => Util.ReadLine());
}

IBlink1 GetBlink()
{
	return Sleddog.Blink1.Blink1Connector.Scan().FirstOrDefault();
}

void Listen(Action fUntil)
{
	var captureCount = 0;
	using (var waveIn = new NAudio.Wave.WasapiLoopbackCapture())
	using (var blink = new WrapDisposable(GetBlink()))
	{
		var sampleThrottle = TimeSpan.FromSeconds(5.5);
		void OnAudio(NAudio.Wave.WaveInEventArgs evArgs)
		{
			captureCount++;
			blink.Item.Blink(System.Drawing.Color.Red, TimeSpan.FromSeconds(1.0), 2);
			(DateTime.Now, evArgs).Dump();
		}
		using (var sub =
			Observable.FromEventPattern<NAudio.Wave.WaveInEventArgs>(target: waveIn, eventName: nameof(waveIn.DataAvailable))
			.Where(evArgs => evArgs.EventArgs.Buffer.Any(b => b != 0)).Sample(sampleThrottle).Select(evArgs => evArgs.EventArgs).Subscribe<WaveInEventArgs>(onNext: OnAudio)
			)
			waveIn.WaveFormat.Dump()
		waveIn.StartRecording();
		fUntil()
		waveIn.StopRecording();
		captureCount.Dump();

	}
}
}

interface IDisposalWrapper<T> : IDisposable
{
	T Item { get; }
	bool IsDisposed { get; }
}

// IBlink has dispose method but doesn't implement IDisposable
class WrapDisposable : IDisposalWrapper<IBlink1>, IDisposable
{
	IBlink1 item;
	public IBlink1 Item
	{
		get
		{
			if (this.IsDisposed)
				throw new InvalidOperationException("Object is disposed");
			return item;
		}
	}
	public bool IsDisposed { get; private set; }
	public void Dispose()
	{
		if (!this.IsDisposed)
		{
			item.Dispose();
			this.IsDisposed = true;
			item = null;
		}
	}

	public WrapDisposable(IBlink1 item) { this.item = item; }
}

@cyberzed cyberzed self-assigned this Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants