feat(lmxproxy): add STA thread with message pump for MxAccess COM callbacks

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-03-22 18:00:24 -04:00
parent c96e71c83c
commit b6408726bc
7 changed files with 161 additions and 17 deletions

View File

@@ -12,7 +12,7 @@ namespace ZB.MOM.WW.LmxProxy.Host.MxAccess
public sealed partial class MxAccessClient
{
/// <summary>
/// Connects to MxAccess via Task.Run (thread pool).
/// Connects to MxAccess on the dedicated STA thread.
/// </summary>
public async Task ConnectAsync(CancellationToken ct = default)
{
@@ -23,7 +23,7 @@ namespace ZB.MOM.WW.LmxProxy.Host.MxAccess
try
{
await Task.Run(() => ConnectInternal(), ct);
await _staThread.RunAsync(() => ConnectInternal());
lock (_lock)
{
@@ -46,7 +46,7 @@ namespace ZB.MOM.WW.LmxProxy.Host.MxAccess
}
/// <summary>
/// Disconnects from MxAccess via Task.Run (thread pool).
/// Disconnects from MxAccess on the dedicated STA thread.
/// </summary>
public async Task DisconnectAsync(CancellationToken ct = default)
{
@@ -56,7 +56,7 @@ namespace ZB.MOM.WW.LmxProxy.Host.MxAccess
try
{
await Task.Run(() => DisconnectInternal());
await _staThread.RunAsync(() => DisconnectInternal());
SetState(ConnectionState.Disconnected);
Log.Information("Disconnected from MxAccess");
@@ -346,13 +346,13 @@ namespace ZB.MOM.WW.LmxProxy.Host.MxAccess
}
/// <summary>
/// Cleans up COM objects via Task.Run after a failed connection.
/// Cleans up COM objects on the dedicated STA thread after a failed connection.
/// </summary>
private async Task CleanupComObjectsAsync()
{
try
{
await Task.Run(() =>
await _staThread.RunAsync(() =>
{
lock (_lock)
{