A Microsoft platform for building and publishing apps for Windows devices.
Hello @Nelphy Rose Siby ,
This is likely a threading context issue. Your native DLL probably expects to be called from a specific thread apartment type (STA). When you call it from the UI thread, it works because the UI thread in UWP is an STA thread. However, when your async socket callback fires, it runs on a background thread pool thread with a different threading context.
Try marshaling the DLL call back to the UI thread. In your socket callback, instead of calling the DLL directly, dispatch the call to the UI thread:
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() => {
// Call your native DLL method here
SCT0M0_0130DLL.ConnectDevice();
});
But in case you want to keep the DLL call on a background thread, you may need to create a new STA thread for the DLL call, something like this:
private Thread _deviceThread;
private BlockingCollection<Action> _deviceQueue = new BlockingCollection<Action>();
// Initialize once at startup
private void InitializeDeviceThread()
{
_deviceThread = new Thread(() =>
{
// Set this thread to STA
foreach (var action in _deviceQueue.GetConsumingEnumerable())
{
try
{
action();
}
catch (Exception ex)
{
// Log error
}
}
});
_deviceThread.SetApartmentState(ApartmentState.STA);
_deviceThread.IsBackground = true;
_deviceThread.Start();
}
// In your socket callback
private Task CallDeviceAsync()
{
var tcs = new TaskCompletionSource<bool>();
_deviceQueue.Add(() =>
{
try
{
SCT0M0_0130DLL.ConnectDevice();
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
}