Files
manual_wpf/Models/TeighaServicesManager.cs
2025-07-22 10:45:51 +09:00

192 lines
6.9 KiB
C#

using System;
using System.Diagnostics;
using Teigha.Runtime;
namespace DwgExtractorManual.Models
{
/// <summary>
/// Singleton class to manage Teigha Services lifecycle and prevent disposal conflicts
/// </summary>
public sealed class TeighaServicesManager
{
private static readonly object _lock = new object();
private static TeighaServicesManager _instance = null;
private static Services _services = null;
private static int _referenceCount = 0;
private static bool _isActivated = false;
private TeighaServicesManager()
{
// Private constructor for singleton
}
public static TeighaServicesManager Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new TeighaServicesManager();
}
}
}
return _instance;
}
}
/// <summary>
/// Acquires Teigha Services (creates if needed, increments reference count)
/// </summary>
/// <returns>The Services instance</returns>
public Services AcquireServices()
{
lock (_lock)
{
try
{
Debug.WriteLine($"[TeighaManager] AcquireServices - Current ref count: {_referenceCount}");
if (!_isActivated)
{
Debug.WriteLine("[TeighaManager] Activating ODA for first time...");
ActivateODA();
_isActivated = true;
}
if (_services == null)
{
Debug.WriteLine("[TeighaManager] Creating new Services instance...");
_services = new Services();
Debug.WriteLine("[TeighaManager] Services instance created successfully");
}
_referenceCount++;
Debug.WriteLine($"[TeighaManager] Services acquired - New ref count: {_referenceCount}");
return _services;
}
catch (Teigha.Runtime.Exception ex)
{
Debug.WriteLine($"[TeighaManager] Error acquiring services: {ex.Message}");
throw;
}
}
}
/// <summary>
/// Releases Teigha Services (decrements reference count, disposes when count reaches 0)
/// </summary>
public void ReleaseServices()
{
lock (_lock)
{
try
{
Debug.WriteLine($"[TeighaManager] ReleaseServices - Current ref count: {_referenceCount}");
if (_referenceCount > 0)
{
_referenceCount--;
Debug.WriteLine($"[TeighaManager] Services released - New ref count: {_referenceCount}");
}
// Don't dispose Services until app shutdown to prevent conflicts
// Just track reference count for debugging
if (_referenceCount == 0)
{
Debug.WriteLine("[TeighaManager] All references released (Services kept alive for app lifetime)");
}
}
catch (Teigha.Runtime.Exception ex)
{
Debug.WriteLine($"[TeighaManager] Error releasing services: {ex.Message}");
// Don't throw on release to prevent cascade failures
}
}
}
/// <summary>
/// Force dispose Services (only call on application shutdown)
/// </summary>
public void ForceDisposeServices()
{
lock (_lock)
{
try
{
Debug.WriteLine("[TeighaManager] Force disposing Services...");
if (_services != null)
{
_services.Dispose();
Debug.WriteLine("[TeighaManager] Services disposed successfully");
}
_services = null;
_referenceCount = 0;
_isActivated = false;
Debug.WriteLine("[TeighaManager] Force dispose completed");
}
catch (Teigha.Runtime.Exception ex)
{
Debug.WriteLine($"[TeighaManager] Error during force dispose: {ex.Message}");
// Reset state even if disposal fails
_services = null;
_referenceCount = 0;
_isActivated = false;
}
}
}
/// <summary>
/// Get current reference count (for debugging)
/// </summary>
public int ReferenceCount
{
get
{
lock (_lock)
{
return _referenceCount;
}
}
}
/// <summary>
/// Check if Services is active and valid
/// </summary>
public bool IsServicesActive
{
get
{
lock (_lock)
{
return _services != null && _isActivated;
}
}
}
private void ActivateODA()
{
try
{
Debug.WriteLine("[TeighaManager] Activating ODA...");
var userInfo = "c2FtYW4gZW5naW5lZXJpbmc=";
var userSignature = "F0kuQTmtVpHtvl/TgaFVGE92/YqGmYR9SLoXckEjnOk8NoAQh7Sg6GQruVC04JqD4C/IipxJYqpqvMfMc2auRMG+cAJCKqKUE2djIMdkUdb+C5IVx2c97fcK5ba3n8DDvB54Upokajl+6j12yD8h8MKGOR3Z3zysObeXD62bFpQgp00GCYTqlxEZtTIRjHIPAfJfix8Y0jtXWWYyVJ3LYOu86as5xtx+hY1aakpYIJiQk/6pGd84qSn/9K1w8nxR7UrFzieDeQ/xM58BHSD4u/ZxVJwvv6Uy10tsdBFBTvfJMAFp05Y7yeyeCNr100tA3iOfmWoXAVRHfxnkPfiYR54aK04QI+R6OGkI+yd1oR5BtmN6BdDt3z8KYK5EpFGJGiJIGoUy5PvkYdJ2VV6xe9JWBiIJuI/tDn1Y+uyTQFA9qaDHnOURriXsRGfy8reDPf1eejybSJxWKkpilG6RXhq3xHlCkjZzh1Q45S+xYXNGatcWMm9nkn20M8Ke5JEVaI9w/p2GE36CHRtRQbt8kfPmsbWNXJCFr4svHW2MPbCKWoyn5XEyMWBnuAKi74zvczB13DKjf29SqSIgF5k/hwy2QrgvnaKzY1k8bw8w2/k0vJXcS3GKOB/ZYDle1tf/lkAD1HtnF9zE18TiXhVnqwAVjwg4ui1RPLn/LMs6b5Y=";
Services.odActivate(userInfo, userSignature);
Debug.WriteLine("[TeighaManager] ODA activation successful");
}
catch (Teigha.Runtime.Exception ex)
{
Debug.WriteLine($"[TeighaManager] ODA activation failed: {ex.Message}");
throw;
}
}
}
}