< Summary

Information
Class: SolidEdgeCommunity.IsolatedTaskProxy
Assembly: SolidEdgeCommunity
File(s): D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\IsolatedTaskProxy.cs
Line coverage
74%
Covered lines: 156
Uncovered lines: 53
Coverable lines: 209
Total lines: 491
Line coverage: 74.6%
Branch coverage
44%
Covered branches: 8
Total branches: 18
Branch coverage: 44.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
InitializeLifetimeService()100%11100%
InvokeSTAThread(...)100%1194.44%
InvokeSTAThread(...)50%2280%
InvokeSTAThread(...)50%2280%
InvokeSTAThread(...)50%2280%
InvokeSTAThread(...)50%2280%
InvokeSTAThread(...)0%620%
InvokeSTAThread(...)50%2280.95%
InvokeSTAThread(...)50%2280.95%
InvokeSTAThread(...)50%2280.95%
InvokeSTAThread(...)50%2285.71%
get_Application()100%11100%
set_Application(...)100%11100%
get_Document()100%11100%
set_Document(...)100%11100%
UnwrapRuntimeCallableWrapper(...)100%11100%

File(s)

D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\IsolatedTaskProxy.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace SolidEdgeCommunity;
 5
 6/// <summary>
 7/// Abstract base class to be used with IsolatedTask&lt;T&gt;.
 8/// </summary>
 9public abstract class IsolatedTaskProxy : MarshalByRefObject
 10{
 11    /// <summary>
 12    /// Lifetime services as disabled by default.
 13    /// </summary>
 14    [Obsolete("Depreciated")]
 115    public override sealed object InitializeLifetimeService() => null;
 16
 17    /// <summary>
 18    /// Invokes a method in a STA thread.
 19    /// </summary>
 20    /// <param name="target"></param>
 21    protected static void InvokeSTAThread(Action target)
 22    {
 123        ArgumentNullException.ThrowIfNull(target);
 24
 125        Exception exception = null;
 26
 27        // Define thread.
 128        Thread thread = new(() =>
 129        {
 130            // Thread specific try\catch.
 131            try
 132            {
 133                target();
 034            }
 135            catch (Exception ex)
 136            {
 137                exception = ex;
 138            }
 239        });
 40
 41        // Important! Set thread apartment state to STA.
 142        thread.SetApartmentState(ApartmentState.STA);
 43
 44        // Start the thread.
 145        thread.Start();
 46
 47        // Wait for the thread to finish.
 148        thread.Join();
 49
 150        throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 51    }
 52
 53    /// <summary>
 54    /// Invokes a method in a STA thread.
 55    /// </summary>
 56    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 57    /// <param name="target"></param>
 58    /// <param name="arg1"></param>
 59    protected static void InvokeSTAThread<TArg1>(Action<TArg1> target, TArg1 arg1)
 60    {
 161        ArgumentNullException.ThrowIfNull(target);
 62
 163        Exception exception = null;
 64
 65        // Define thread.
 166        var thread = new Thread(() =>
 167        {
 168            // Thread specific try\catch.
 169            try
 170            {
 171                target(arg1);
 172            }
 073            catch (Exception ex)
 174            {
 075                exception = ex;
 076            }
 277        });
 78
 79        // Important! Set thread apartment state to STA.
 180        thread.SetApartmentState(ApartmentState.STA);
 81
 82        // Start the thread.
 183        thread.Start();
 84
 85        // Wait for the thread to finish.
 186        thread.Join();
 87
 188        if (exception != null)
 89        {
 090            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 91        }
 192    }
 93
 94    /// <summary>
 95    /// Invokes a method in a STA thread.
 96    /// </summary>
 97    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 98    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 99    /// <param name="target"></param>
 100    /// <param name="arg1"></param>
 101    /// <param name="arg2"></param>
 102    protected static void InvokeSTAThread<TArg1, TArg2>(Action<TArg1, TArg2> target, TArg1 arg1, TArg2 arg2)
 103    {
 1104        ArgumentNullException.ThrowIfNull(target);
 105
 1106        Exception exception = null;
 107
 108        // Define thread.
 1109        var thread = new Thread(() =>
 1110        {
 1111            // Thread specific try\catch.
 1112            try
 1113            {
 1114                target(arg1, arg2);
 1115            }
 0116            catch (Exception ex)
 1117            {
 0118                exception = ex;
 0119            }
 2120        });
 121
 122        // Important! Set thread apartment state to STA.
 1123        thread.SetApartmentState(ApartmentState.STA);
 124
 125        // Start the thread.
 1126        thread.Start();
 127
 128        // Wait for the thread to finish.
 1129        thread.Join();
 130
 1131        if (exception != null)
 132        {
 0133            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 134        }
 1135    }
 136
 137    /// <summary>
 138    /// Invokes a method in a STA thread.
 139    /// </summary>
 140    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 141    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 142    /// <typeparam name="TArg3">The type of arg3.</typeparam>
 143    /// <param name="target"></param>
 144    /// <param name="arg1"></param>
 145    /// <param name="arg2"></param>
 146    /// <param name="arg3"></param>
 147    protected static void InvokeSTAThread<TArg1, TArg2, TArg3>(Action<TArg1, TArg2, TArg3> target, TArg1 arg1, TArg2 arg
 148    {
 1149        ArgumentNullException.ThrowIfNull(target);
 150
 1151        Exception exception = null;
 152
 153        // Define thread.
 1154        var thread = new Thread(() =>
 1155        {
 1156            // Thread specific try\catch.
 1157            try
 1158            {
 1159                target(arg1, arg2, arg3);
 1160            }
 0161            catch (Exception ex)
 1162            {
 0163                exception = ex;
 0164            }
 2165        });
 166
 167        // Important! Set thread apartment state to STA.
 1168        thread.SetApartmentState(ApartmentState.STA);
 169
 170        // Start the thread.
 1171        thread.Start();
 172
 173        // Wait for the thread to finish.
 1174        thread.Join();
 175
 1176        if (exception != null)
 177        {
 0178            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 179        }
 1180    }
 181
 182    /// <summary>
 183    /// Invokes a method in a STA thread.
 184    /// </summary>
 185    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 186    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 187    /// <typeparam name="TArg3">The type of arg3.</typeparam>
 188    /// <typeparam name="TArg4">The type of arg4.</typeparam>
 189    /// <param name="target"></param>
 190    /// <param name="arg1"></param>
 191    /// <param name="arg2"></param>
 192    /// <param name="arg3"></param>
 193    /// <param name="arg4"></param>
 194    protected static void InvokeSTAThread<TArg1, TArg2, TArg3, TArg4>(Action<TArg1, TArg2, TArg3, TArg4> target, TArg1 a
 195    {
 1196        ArgumentNullException.ThrowIfNull(target);
 197
 1198        Exception exception = null;
 199
 200        // Define thread.
 1201        var thread = new Thread(() =>
 1202        {
 1203            // Thread specific try\catch.
 1204            try
 1205            {
 1206                target(arg1, arg2, arg3, arg4);
 1207            }
 0208            catch (Exception ex)
 1209            {
 0210                exception = ex;
 0211            }
 2212        });
 213
 214        // Important! Set thread apartment state to STA.
 1215        thread.SetApartmentState(ApartmentState.STA);
 216
 217        // Start the thread.
 1218        thread.Start();
 219
 220        // Wait for the thread to finish.
 1221        thread.Join();
 222
 1223        if (exception != null)
 224        {
 0225            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 226        }
 1227    }
 228
 229    /// <summary>
 230    /// Invokes a method in a STA thread.
 231    /// </summary>
 232    /// <typeparam name="TResult">The type of the return value.</typeparam>
 233    /// <param name="target"></param>
 234    /// <returns>An instance of TResult.</returns>
 235    protected static TResult InvokeSTAThread<TResult>(Func<TResult> target)
 236    {
 0237        ArgumentNullException.ThrowIfNull(target);
 238
 0239        TResult returnValue = default;
 0240        Exception exception = null;
 241
 242        // Define thread.
 0243        var thread = new Thread(() =>
 0244        {
 0245            // Thread specific try\catch.
 0246            try
 0247            {
 0248                returnValue = target();
 0249            }
 0250            catch (Exception ex)
 0251            {
 0252                exception = ex;
 0253            }
 0254        });
 255
 256        // Important! Set thread apartment state to STA.
 0257        thread.SetApartmentState(ApartmentState.STA);
 258
 259        // Start the thread.
 0260        thread.Start();
 261
 262        // Wait for the thread to finish.
 0263        thread.Join();
 264
 0265        if (exception != null)
 266        {
 0267            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 268        }
 269
 0270        return returnValue;
 271    }
 272
 273    /// <summary>
 274    /// Invokes a method in a STA thread.
 275    /// </summary>
 276    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 277    /// <typeparam name="TResult">The type of the return value.</typeparam>
 278    /// <param name="target"></param>
 279    /// <param name="arg1"></param>
 280    /// <returns>An instance of TResult.</returns>
 281    protected static TResult InvokeSTAThread<TArg1, TResult>(Func<TArg1, TResult> target, TArg1 arg1)
 282    {
 1283        ArgumentNullException.ThrowIfNull(target);
 284
 1285        TResult returnValue = default;
 1286        Exception exception = null;
 287
 288        // Define thread.
 1289        var thread = new Thread(() =>
 1290        {
 1291            // Thread specific try\catch.
 1292            try
 1293            {
 1294                returnValue = target(arg1);
 1295            }
 0296            catch (Exception ex)
 1297            {
 0298                exception = ex;
 0299            }
 2300        });
 301
 302        // Important! Set thread apartment state to STA.
 1303        thread.SetApartmentState(ApartmentState.STA);
 304
 305        // Start the thread.
 1306        thread.Start();
 307
 308        // Wait for the thread to finish.
 1309        thread.Join();
 310
 1311        if (exception != null)
 312        {
 0313            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 314        }
 315
 1316        return returnValue;
 317    }
 318
 319    /// <summary>
 320    /// Invokes a method in a STA thread.
 321    /// </summary>
 322    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 323    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 324    /// <typeparam name="TResult">The type of the return value.</typeparam>
 325    /// <param name="target"></param>
 326    /// <param name="arg1"></param>
 327    /// <param name="arg2"></param>
 328    /// <returns>An instance of TResult.</returns>
 329    protected static TResult InvokeSTAThread<TArg1, TArg2, TResult>(Func<TArg1, TArg2, TResult> target, TArg1 arg1, TArg
 330    {
 1331        ArgumentNullException.ThrowIfNull(target);
 332
 1333        TResult returnValue = default;
 1334        Exception exception = null;
 335
 336        // Define thread.
 1337        var thread = new Thread(() =>
 1338        {
 1339            // Thread specific try\catch.
 1340            try
 1341            {
 1342                returnValue = target(arg1, arg2);
 1343            }
 0344            catch (Exception ex)
 1345            {
 0346                exception = ex;
 0347            }
 2348        });
 349
 350        // Important! Set thread apartment state to STA.
 1351        thread.SetApartmentState(ApartmentState.STA);
 352
 353        // Start the thread.
 1354        thread.Start();
 355
 356        // Wait for the thread to finish.
 1357        thread.Join();
 358
 1359        if (exception != null)
 360        {
 0361            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 362        }
 363
 1364        return returnValue;
 365    }
 366
 367    /// <summary>
 368    /// Invokes a method in a STA thread.
 369    /// </summary>
 370    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 371    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 372    /// <typeparam name="TArg3">The type of arg3.</typeparam>
 373    /// <typeparam name="TResult">The type of the return value.</typeparam>
 374    /// <param name="target"></param>
 375    /// <param name="arg1"></param>
 376    /// <param name="arg2"></param>
 377    /// <param name="arg3"></param>
 378    /// <returns>An instance of TResult.</returns>
 379    protected static TResult InvokeSTAThread<TArg1, TArg2, TArg3, TResult>(Func<TArg1, TArg2, TArg3, TResult> target, TA
 380    {
 1381        ArgumentNullException.ThrowIfNull(target);
 382
 1383        TResult returnValue = default;
 1384        Exception exception = null;
 385
 386        // Define thread.
 1387        var thread = new Thread(() =>
 1388        {
 1389            // Thread specific try\catch.
 1390            try
 1391            {
 1392                returnValue = target(arg1, arg2, arg3);
 1393            }
 0394            catch (Exception ex)
 1395            {
 0396                exception = ex;
 0397            }
 2398        });
 399
 400        // Important! Set thread apartment state to STA.
 1401        thread.SetApartmentState(ApartmentState.STA);
 402
 403        // Start the thread.
 1404        thread.Start();
 405
 406        // Wait for the thread to finish.
 1407        thread.Join();
 408
 1409        if (exception != null)
 410        {
 0411            throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception);
 412        }
 413
 1414        return returnValue;
 415    }
 416
 417    /// <summary>
 418    /// Invokes a method in a STA thread.
 419    /// </summary>
 420    /// <typeparam name="TArg1">The type of arg1.</typeparam>
 421    /// <typeparam name="TArg2">The type of arg2.</typeparam>
 422    /// <typeparam name="TArg3">The type of arg3.</typeparam>
 423    /// <typeparam name="TArg4">The type of arg4.</typeparam>
 424    /// <typeparam name="TResult">The type of the return value.</typeparam>
 425    /// <param name="target"></param>
 426    /// <param name="arg1"></param>
 427    /// <param name="arg2"></param>
 428    /// <param name="arg3"></param>
 429    /// <param name="arg4"></param>
 430    /// <returns>An instance of TResult.</returns>
 431    protected static TResult InvokeSTAThread<TArg1, TArg2, TArg3, TArg4, TResult>(Func<TArg1, TArg2, TArg3, TArg4, TResu
 432    {
 1433        ArgumentNullException.ThrowIfNull(target);
 434
 1435        TResult returnValue = default;
 1436        Exception exception = null;
 437
 438        // Define thread.
 1439        var thread = new Thread(() =>
 1440        {
 1441            // Thread specific try\catch.
 1442            try
 1443            {
 1444                returnValue = target(arg1, arg2, arg3, arg4);
 1445            }
 0446            catch (Exception ex)
 1447            {
 0448                exception = ex;
 0449            }
 2450        });
 451
 452        // Important! Set thread apartment state to STA.
 1453        thread.SetApartmentState(System.Threading.ApartmentState.STA);
 454
 455        // Start the thread.
 1456        thread.Start();
 457
 458        // Wait for the thread to finish.
 1459        thread.Join();
 460
 1461        return exception != null
 1462            ? throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception)
 1463            : returnValue;
 464    }
 465
 466    /// <summary>
 467    /// Solid Edge Application property.
 468    /// </summary>
 469    public Application Application
 470    {
 1471        get;
 1472        set => field = UnwrapRuntimeCallableWrapper<Application>(value);
 473    }
 474
 475    /// <summary>
 476    /// Solid Edge Application property.
 477    /// </summary>
 478    public SolidEdgeDocument Document
 479    {
 1480        get;
 1481        set => field = UnwrapRuntimeCallableWrapper<SolidEdgeDocument>(value);
 482    }
 483
 484    /// <summary>
 485    /// Unwraps a runtime callable wrapper (RCW) that is passed across AppDomains.
 486    /// </summary>
 487    /// <typeparam name="TInterface"></typeparam>
 488    /// <param name="rcw"></param>
 489    /// <returns></returns>
 4490    protected static TInterface UnwrapRuntimeCallableWrapper<TInterface>(object rcw) where TInterface : class => rcw as 
 491}