- Home /
IDevice.InitializeAsync().Wait() throws I/O Exception in System.FileStream
Hey everyone,
I'm struggling with a I/O Exception when calling IDevice.InitializeAsync().Wait(); in a new thread.
I am using the Hid.Net.dll and after a bit of research I found out, that there are some general problems with SafeFileHandlers/HID in Unity. I Changed Unitys API compatibility Level to 4.x but it still didn't work.
It seems to be a problem which I cannot really influence, since the error occurs within constructors/methods of System.FileStream classes which I obviously can't change. I already tried to understand and even change some of the code snippets using dotPeek and dnSpy. So I hope that I'm wrong and the problem rather is in the setup, before even calling the constructors etc.
(Btw. I'm using the code of Mxster which natively runs in VS C#, so the problem only occurs when using his .dll in Unity.)
Here is my code so far:
Instead of a static Bootstrapping method I changed this class to have normal constructor: This code works perfectly fine, the controller is recognized!
public Devices()
{
WindowsHidDeviceFactory.Register(_logger, _tracer);
WindowsUsbDeviceFactory.Register(_logger, _tracer);
SearchDS5Controller();
}
public void SearchDS5Controller()
{
var devices = EnumerateDevices().Item1;
foreach (var deviceInfo in devicesList.Where(deviceInfo => deviceInfo.Device.IsInitialized))
{
deviceInfo.Close();
}
Debug.Log(devices.Count());
foreach (var deviceInfo in devices)
{
devicesList.Add(deviceInfo);
deviceInfo.Init();
}
}
In the above code deviceInfo.Init() calls the following function, which creates a thread starts it and calls IDevice.InitializeAsync().Wait(); This is the method that throws the exception:
public void Init()
{
var thread = new Thread(() =>
{
Device.InitializeAsync().Wait();
});
thread.Start();
thread.Join();
_timeoutCheckThread = new Thread(TimeoutTestThread);
_timeoutCheckThread.Priority = System.Threading.ThreadPriority.BelowNormal;
_timeoutCheckThread.Name = "DualSense Timeout thread: " + Device.DeviceId;
_timeoutCheckThread.IsBackground = true;
_timeoutCheckThread.Start();
}
Exception:
IOException: Invalid handle. System.IO.FileStream.Init (Microsoft.Win32.SafeHandles.SafeFileHandle safeHandle, System.IO.FileAccess access, System.Boolean ownsHandle, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean isConsoleWrapper) (at :0) System.IO.FileStream..ctor (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access, System.Int32 bufferSize, System.Boolean isAsync) (at :0) (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(Microsoft.Win32.SafeHandles.SafeFileHandle,System.IO.FileAccess,int,bool) Hid.Net.Windows.WindowsHidApiService.OpenRead (Microsoft.Win32.SafeHandles.SafeFileHandle readSafeFileHandle, System.UInt16 readBufferSize) (at :0) Hid.Net.Windows.WindowsHidDevice.Initialize () (at :0) Hid.Net.Windows.WindowsHidDevice.b_36_0 () (at :0) System.Threading.Tasks.Task`1[TResult].InnerInvoke () (at :0) System.Threading.Tasks.Task.Execute () (at :0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at :0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at :0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at :0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at :0) System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at :0) Hid.Net.Windows.WindowsHidDevice+d36.MoveNext () (at :0) Rethrow as AggregateException: One or more errors occurred. System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (at :0) System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) (at :0) System.Threading.Tasks.Task.Wait () (at :0) DualSenseSupport.DeviceInfo.b20_0 () (at Assets/Plugins/DualSenseSupport/DeviceInfo.cs:189) System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) (at :0) System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at :0) System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) (at :0) System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) (at :0) System.Threading.ThreadHelper.ThreadStart () (at :0) UnityEngine.<>c:b_0_0(Object, UnhandledExceptionEventArgs)
If you do have any suggestions I would appreciate a short answer!
Cheers :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity Jobs Unknown Length of NativeArray 0 Answers
3 person controler. rotate and turn 0 Answers
Need help with controller script. 1 Answer