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

Improve compatibility with .NET Core and later (including .NET 5 and above) #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/HidLibrary/HidAsyncState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
namespace HidLibrary
{
#if !NETCOREAPP1_0_OR_GREATER
// data provided to delegate BeginInvoke callbacks
// no longer needed for .NET Core and later (includes .NET 5 and above) since BeginInvoke and EndInvoke delegate
// calls are not supported, and Task<> is used instead

public class HidAsyncState
{
private readonly object _callerDelegate;
Expand All @@ -14,4 +19,5 @@ public HidAsyncState(object callerDelegate, object callbackDelegate)
public object CallerDelegate { get { return _callerDelegate; } }
public object CallbackDelegate { get { return _callbackDelegate; } }
}
#endif
}
57 changes: 45 additions & 12 deletions src/HidLibrary/HidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,26 @@ public void Read(ReadCallback callback)

public void Read(ReadCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with Read() and call callback afterwards
Task<HidDeviceData> readTask = Task.Run(() => Read(timeout));
readTask.ContinueWith(task => callback.Invoke(task.Result));
#else
// Note: delegate BeginInvoke calls are not supported in .NET Core and later (includes .NET 5 and above),
// see e.g. https://devblogs.microsoft.com/dotnet/migrating-delegate-begininvoke-calls-for-net-core/

var readDelegate = new ReadDelegate(Read);
var asyncState = new HidAsyncState(readDelegate, callback);
readDelegate.BeginInvoke(timeout, EndRead, asyncState);
#endif
}

public async Task<HidDeviceData> ReadAsync(int timeout = 0)
{
var readDelegate = new ReadDelegate(Read);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<HidDeviceData>.Factory.StartNew(() => readDelegate.Invoke(timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<HidDeviceData>.Factory.StartNew(() => Read(timeout));
#else
var readDelegate = new ReadDelegate(Read);
return await Task<HidDeviceData>.Factory.FromAsync(readDelegate.BeginInvoke, readDelegate.EndInvoke, timeout, null);
#endif
}
Expand All @@ -178,17 +187,23 @@ public void ReadReport(ReadReportCallback callback)

public void ReadReport(ReadReportCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with ReadReport() and call callback afterwards
Task<HidReport> readReportTask = Task.Run(() => ReadReport(timeout));
readReportTask.ContinueWith(task => callback.Invoke(task.Result));
#else
var readReportDelegate = new ReadReportDelegate(ReadReport);
var asyncState = new HidAsyncState(readReportDelegate, callback);
readReportDelegate.BeginInvoke(timeout, EndReadReport, asyncState);
#endif
}

public async Task<HidReport> ReadReportAsync(int timeout = 0)
{
var readReportDelegate = new ReadReportDelegate(ReadReport);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<HidReport>.Factory.StartNew(() => readReportDelegate.Invoke(timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<HidReport>.Factory.StartNew(() => ReadReport(timeout));
#else
var readReportDelegate = new ReadReportDelegate(ReadReport);
return await Task<HidReport>.Factory.FromAsync(readReportDelegate.BeginInvoke, readReportDelegate.EndInvoke, timeout, null);
#endif
}
Expand Down Expand Up @@ -361,17 +376,23 @@ public void Write(byte[] data, WriteCallback callback)

public void Write(byte[] data, WriteCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with Write() and call callback afterwards
Task<bool> writeTask = Task.Run(() => Write(data, timeout));
writeTask.ContinueWith(task => callback.Invoke(task.Result));
#else
var writeDelegate = new WriteDelegate(Write);
var asyncState = new HidAsyncState(writeDelegate, callback);
writeDelegate.BeginInvoke(data, timeout, EndWrite, asyncState);
#endif
}

public async Task<bool> WriteAsync(byte[] data, int timeout = 0)
{
var writeDelegate = new WriteDelegate(Write);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<bool>.Factory.StartNew(() => writeDelegate.Invoke(data, timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<bool>.Factory.StartNew(() => Write(data, timeout));
#else
var writeDelegate = new WriteDelegate(Write);
return await Task<bool>.Factory.FromAsync(writeDelegate.BeginInvoke, writeDelegate.EndInvoke, data, timeout, null);
#endif
}
Expand All @@ -393,9 +414,15 @@ public void WriteReport(HidReport report, WriteCallback callback)

public void WriteReport(HidReport report, WriteCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with WriteReport() and call callback afterwards
Task<bool> writeReportTask = Task.Run(() => WriteReport(report, timeout));
writeReportTask.ContinueWith(task => callback.Invoke(task.Result));
#else
var writeReportDelegate = new WriteReportDelegate(WriteReport);
var asyncState = new HidAsyncState(writeReportDelegate, callback);
writeReportDelegate.BeginInvoke(report, timeout, EndWriteReport, asyncState);
#endif
}

/// <summary>
Expand All @@ -419,10 +446,10 @@ public bool WriteReportSync(HidReport report)

public async Task<bool> WriteReportAsync(HidReport report, int timeout = 0)
{
var writeReportDelegate = new WriteReportDelegate(WriteReport);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<bool>.Factory.StartNew(() => writeReportDelegate.Invoke(report, timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<bool>.Factory.StartNew(() => WriteReport(report, timeout));
#else
var writeReportDelegate = new WriteReportDelegate(WriteReport);
return await Task<bool>.Factory.FromAsync(writeReportDelegate.BeginInvoke, writeReportDelegate.EndInvoke, report, timeout, null);
#endif
}
Expand Down Expand Up @@ -465,6 +492,11 @@ public bool WriteFeatureData(byte[] data)
return success;
}

#if !NETCOREAPP1_0_OR_GREATER
// callbacks provided to delegate BeginInvoke
// no longer needed for .NET Core and later (includes .NET 5 and above) since BeginInvoke and EndInvoke
// delegate calls are not supported, and Task<> is used instead

protected static void EndRead(IAsyncResult ar)
{
var hidAsyncState = (HidAsyncState)ar.AsyncState;
Expand Down Expand Up @@ -504,6 +536,7 @@ private static void EndWriteReport(IAsyncResult ar)

if ((callbackDelegate != null)) callbackDelegate.Invoke(result);
}
#endif

private byte[] CreateInputBuffer()
{
Expand Down
2 changes: 1 addition & 1 deletion src/HidLibrary/HidDeviceEventMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public HidDeviceEventMonitor(HidDevice device)

public void Init()
{
#if NET20 || NET35 || NET5_0_OR_GREATER
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
Task task = Task.Factory.StartNew(() => DeviceEventMonitor());
#else
var eventMonitor = new Action(DeviceEventMonitor);
Expand Down
24 changes: 18 additions & 6 deletions src/HidLibrary/HidFastReadDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ public void FastRead(ReadCallback callback)

public void FastRead(ReadCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with FastRead() and call callback afterwards
Task<HidDeviceData> readTask = Task.Run(() => FastRead(timeout));
readTask.ContinueWith(task => callback.Invoke(task.Result));
#else
var readDelegate = new ReadDelegate(FastRead);
var asyncState = new HidAsyncState(readDelegate, callback);
readDelegate.BeginInvoke(timeout, EndRead, asyncState);
#endif
}

public async Task<HidDeviceData> FastReadAsync(int timeout = 0)
{
var readDelegate = new ReadDelegate(FastRead);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<HidDeviceData>.Factory.StartNew(() => readDelegate.Invoke(timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<HidDeviceData>.Factory.StartNew(() => FastRead(timeout));
#else
var readDelegate = new ReadDelegate(FastRead);
return await Task<HidDeviceData>.Factory.FromAsync(readDelegate.BeginInvoke, readDelegate.EndInvoke, timeout, null);
#endif
}
Expand All @@ -66,17 +72,23 @@ public void FastReadReport(ReadReportCallback callback)

public void FastReadReport(ReadReportCallback callback, int timeout)
{
#if NETCOREAPP1_0_OR_GREATER
// start new task with FastReadReport() and call callback afterwards
Task<HidReport> readReportTask = Task.Run(() => FastReadReport(timeout));
readReportTask.ContinueWith(task => callback.Invoke(task.Result));
#else
var readReportDelegate = new ReadReportDelegate(FastReadReport);
var asyncState = new HidAsyncState(readReportDelegate, callback);
readReportDelegate.BeginInvoke(timeout, EndReadReport, asyncState);
#endif
}

public async Task<HidReport> FastReadReportAsync(int timeout = 0)
{
var readReportDelegate = new ReadReportDelegate(FastReadReport);
#if NET20 || NET35 || NET5_0_OR_GREATER
return await Task<HidReport>.Factory.StartNew(() => readReportDelegate.Invoke(timeout));
#if NET20 || NET35 || NETCOREAPP1_0_OR_GREATER
return await Task<HidReport>.Factory.StartNew(() => FastReadReport(timeout));
#else
var readReportDelegate = new ReadReportDelegate(FastReadReport);
return await Task<HidReport>.Factory.FromAsync(readReportDelegate.BeginInvoke, readReportDelegate.EndInvoke, timeout, null);
#endif
}
Expand Down
Loading