| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SolidEdgeCommunity; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Abstract base class to be used with IsolatedTask<T>. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class IsolatedTaskProxy : MarshalByRefObject |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Lifetime services as disabled by default. |
| | | 13 | | /// </summary> |
| | | 14 | | [Obsolete("Depreciated")] |
| | 1 | 15 | | 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 | | { |
| | 1 | 23 | | ArgumentNullException.ThrowIfNull(target); |
| | | 24 | | |
| | 1 | 25 | | Exception exception = null; |
| | | 26 | | |
| | | 27 | | // Define thread. |
| | 1 | 28 | | Thread thread = new(() => |
| | 1 | 29 | | { |
| | 1 | 30 | | // Thread specific try\catch. |
| | 1 | 31 | | try |
| | 1 | 32 | | { |
| | 1 | 33 | | target(); |
| | 0 | 34 | | } |
| | 1 | 35 | | catch (Exception ex) |
| | 1 | 36 | | { |
| | 1 | 37 | | exception = ex; |
| | 1 | 38 | | } |
| | 2 | 39 | | }); |
| | | 40 | | |
| | | 41 | | // Important! Set thread apartment state to STA. |
| | 1 | 42 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 43 | | |
| | | 44 | | // Start the thread. |
| | 1 | 45 | | thread.Start(); |
| | | 46 | | |
| | | 47 | | // Wait for the thread to finish. |
| | 1 | 48 | | thread.Join(); |
| | | 49 | | |
| | 1 | 50 | | 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 | | { |
| | 1 | 61 | | ArgumentNullException.ThrowIfNull(target); |
| | | 62 | | |
| | 1 | 63 | | Exception exception = null; |
| | | 64 | | |
| | | 65 | | // Define thread. |
| | 1 | 66 | | var thread = new Thread(() => |
| | 1 | 67 | | { |
| | 1 | 68 | | // Thread specific try\catch. |
| | 1 | 69 | | try |
| | 1 | 70 | | { |
| | 1 | 71 | | target(arg1); |
| | 1 | 72 | | } |
| | 0 | 73 | | catch (Exception ex) |
| | 1 | 74 | | { |
| | 0 | 75 | | exception = ex; |
| | 0 | 76 | | } |
| | 2 | 77 | | }); |
| | | 78 | | |
| | | 79 | | // Important! Set thread apartment state to STA. |
| | 1 | 80 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 81 | | |
| | | 82 | | // Start the thread. |
| | 1 | 83 | | thread.Start(); |
| | | 84 | | |
| | | 85 | | // Wait for the thread to finish. |
| | 1 | 86 | | thread.Join(); |
| | | 87 | | |
| | 1 | 88 | | if (exception != null) |
| | | 89 | | { |
| | 0 | 90 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 91 | | } |
| | 1 | 92 | | } |
| | | 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 | | { |
| | 1 | 104 | | ArgumentNullException.ThrowIfNull(target); |
| | | 105 | | |
| | 1 | 106 | | Exception exception = null; |
| | | 107 | | |
| | | 108 | | // Define thread. |
| | 1 | 109 | | var thread = new Thread(() => |
| | 1 | 110 | | { |
| | 1 | 111 | | // Thread specific try\catch. |
| | 1 | 112 | | try |
| | 1 | 113 | | { |
| | 1 | 114 | | target(arg1, arg2); |
| | 1 | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | 1 | 117 | | { |
| | 0 | 118 | | exception = ex; |
| | 0 | 119 | | } |
| | 2 | 120 | | }); |
| | | 121 | | |
| | | 122 | | // Important! Set thread apartment state to STA. |
| | 1 | 123 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 124 | | |
| | | 125 | | // Start the thread. |
| | 1 | 126 | | thread.Start(); |
| | | 127 | | |
| | | 128 | | // Wait for the thread to finish. |
| | 1 | 129 | | thread.Join(); |
| | | 130 | | |
| | 1 | 131 | | if (exception != null) |
| | | 132 | | { |
| | 0 | 133 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 134 | | } |
| | 1 | 135 | | } |
| | | 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 | | { |
| | 1 | 149 | | ArgumentNullException.ThrowIfNull(target); |
| | | 150 | | |
| | 1 | 151 | | Exception exception = null; |
| | | 152 | | |
| | | 153 | | // Define thread. |
| | 1 | 154 | | var thread = new Thread(() => |
| | 1 | 155 | | { |
| | 1 | 156 | | // Thread specific try\catch. |
| | 1 | 157 | | try |
| | 1 | 158 | | { |
| | 1 | 159 | | target(arg1, arg2, arg3); |
| | 1 | 160 | | } |
| | 0 | 161 | | catch (Exception ex) |
| | 1 | 162 | | { |
| | 0 | 163 | | exception = ex; |
| | 0 | 164 | | } |
| | 2 | 165 | | }); |
| | | 166 | | |
| | | 167 | | // Important! Set thread apartment state to STA. |
| | 1 | 168 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 169 | | |
| | | 170 | | // Start the thread. |
| | 1 | 171 | | thread.Start(); |
| | | 172 | | |
| | | 173 | | // Wait for the thread to finish. |
| | 1 | 174 | | thread.Join(); |
| | | 175 | | |
| | 1 | 176 | | if (exception != null) |
| | | 177 | | { |
| | 0 | 178 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 179 | | } |
| | 1 | 180 | | } |
| | | 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 | | { |
| | 1 | 196 | | ArgumentNullException.ThrowIfNull(target); |
| | | 197 | | |
| | 1 | 198 | | Exception exception = null; |
| | | 199 | | |
| | | 200 | | // Define thread. |
| | 1 | 201 | | var thread = new Thread(() => |
| | 1 | 202 | | { |
| | 1 | 203 | | // Thread specific try\catch. |
| | 1 | 204 | | try |
| | 1 | 205 | | { |
| | 1 | 206 | | target(arg1, arg2, arg3, arg4); |
| | 1 | 207 | | } |
| | 0 | 208 | | catch (Exception ex) |
| | 1 | 209 | | { |
| | 0 | 210 | | exception = ex; |
| | 0 | 211 | | } |
| | 2 | 212 | | }); |
| | | 213 | | |
| | | 214 | | // Important! Set thread apartment state to STA. |
| | 1 | 215 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 216 | | |
| | | 217 | | // Start the thread. |
| | 1 | 218 | | thread.Start(); |
| | | 219 | | |
| | | 220 | | // Wait for the thread to finish. |
| | 1 | 221 | | thread.Join(); |
| | | 222 | | |
| | 1 | 223 | | if (exception != null) |
| | | 224 | | { |
| | 0 | 225 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 226 | | } |
| | 1 | 227 | | } |
| | | 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 | | { |
| | 0 | 237 | | ArgumentNullException.ThrowIfNull(target); |
| | | 238 | | |
| | 0 | 239 | | TResult returnValue = default; |
| | 0 | 240 | | Exception exception = null; |
| | | 241 | | |
| | | 242 | | // Define thread. |
| | 0 | 243 | | var thread = new Thread(() => |
| | 0 | 244 | | { |
| | 0 | 245 | | // Thread specific try\catch. |
| | 0 | 246 | | try |
| | 0 | 247 | | { |
| | 0 | 248 | | returnValue = target(); |
| | 0 | 249 | | } |
| | 0 | 250 | | catch (Exception ex) |
| | 0 | 251 | | { |
| | 0 | 252 | | exception = ex; |
| | 0 | 253 | | } |
| | 0 | 254 | | }); |
| | | 255 | | |
| | | 256 | | // Important! Set thread apartment state to STA. |
| | 0 | 257 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 258 | | |
| | | 259 | | // Start the thread. |
| | 0 | 260 | | thread.Start(); |
| | | 261 | | |
| | | 262 | | // Wait for the thread to finish. |
| | 0 | 263 | | thread.Join(); |
| | | 264 | | |
| | 0 | 265 | | if (exception != null) |
| | | 266 | | { |
| | 0 | 267 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | 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 | | { |
| | 1 | 283 | | ArgumentNullException.ThrowIfNull(target); |
| | | 284 | | |
| | 1 | 285 | | TResult returnValue = default; |
| | 1 | 286 | | Exception exception = null; |
| | | 287 | | |
| | | 288 | | // Define thread. |
| | 1 | 289 | | var thread = new Thread(() => |
| | 1 | 290 | | { |
| | 1 | 291 | | // Thread specific try\catch. |
| | 1 | 292 | | try |
| | 1 | 293 | | { |
| | 1 | 294 | | returnValue = target(arg1); |
| | 1 | 295 | | } |
| | 0 | 296 | | catch (Exception ex) |
| | 1 | 297 | | { |
| | 0 | 298 | | exception = ex; |
| | 0 | 299 | | } |
| | 2 | 300 | | }); |
| | | 301 | | |
| | | 302 | | // Important! Set thread apartment state to STA. |
| | 1 | 303 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 304 | | |
| | | 305 | | // Start the thread. |
| | 1 | 306 | | thread.Start(); |
| | | 307 | | |
| | | 308 | | // Wait for the thread to finish. |
| | 1 | 309 | | thread.Join(); |
| | | 310 | | |
| | 1 | 311 | | if (exception != null) |
| | | 312 | | { |
| | 0 | 313 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 314 | | } |
| | | 315 | | |
| | 1 | 316 | | 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 | | { |
| | 1 | 331 | | ArgumentNullException.ThrowIfNull(target); |
| | | 332 | | |
| | 1 | 333 | | TResult returnValue = default; |
| | 1 | 334 | | Exception exception = null; |
| | | 335 | | |
| | | 336 | | // Define thread. |
| | 1 | 337 | | var thread = new Thread(() => |
| | 1 | 338 | | { |
| | 1 | 339 | | // Thread specific try\catch. |
| | 1 | 340 | | try |
| | 1 | 341 | | { |
| | 1 | 342 | | returnValue = target(arg1, arg2); |
| | 1 | 343 | | } |
| | 0 | 344 | | catch (Exception ex) |
| | 1 | 345 | | { |
| | 0 | 346 | | exception = ex; |
| | 0 | 347 | | } |
| | 2 | 348 | | }); |
| | | 349 | | |
| | | 350 | | // Important! Set thread apartment state to STA. |
| | 1 | 351 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 352 | | |
| | | 353 | | // Start the thread. |
| | 1 | 354 | | thread.Start(); |
| | | 355 | | |
| | | 356 | | // Wait for the thread to finish. |
| | 1 | 357 | | thread.Join(); |
| | | 358 | | |
| | 1 | 359 | | if (exception != null) |
| | | 360 | | { |
| | 0 | 361 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 362 | | } |
| | | 363 | | |
| | 1 | 364 | | 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 | | { |
| | 1 | 381 | | ArgumentNullException.ThrowIfNull(target); |
| | | 382 | | |
| | 1 | 383 | | TResult returnValue = default; |
| | 1 | 384 | | Exception exception = null; |
| | | 385 | | |
| | | 386 | | // Define thread. |
| | 1 | 387 | | var thread = new Thread(() => |
| | 1 | 388 | | { |
| | 1 | 389 | | // Thread specific try\catch. |
| | 1 | 390 | | try |
| | 1 | 391 | | { |
| | 1 | 392 | | returnValue = target(arg1, arg2, arg3); |
| | 1 | 393 | | } |
| | 0 | 394 | | catch (Exception ex) |
| | 1 | 395 | | { |
| | 0 | 396 | | exception = ex; |
| | 0 | 397 | | } |
| | 2 | 398 | | }); |
| | | 399 | | |
| | | 400 | | // Important! Set thread apartment state to STA. |
| | 1 | 401 | | thread.SetApartmentState(ApartmentState.STA); |
| | | 402 | | |
| | | 403 | | // Start the thread. |
| | 1 | 404 | | thread.Start(); |
| | | 405 | | |
| | | 406 | | // Wait for the thread to finish. |
| | 1 | 407 | | thread.Join(); |
| | | 408 | | |
| | 1 | 409 | | if (exception != null) |
| | | 410 | | { |
| | 0 | 411 | | throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception); |
| | | 412 | | } |
| | | 413 | | |
| | 1 | 414 | | 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 | | { |
| | 1 | 433 | | ArgumentNullException.ThrowIfNull(target); |
| | | 434 | | |
| | 1 | 435 | | TResult returnValue = default; |
| | 1 | 436 | | Exception exception = null; |
| | | 437 | | |
| | | 438 | | // Define thread. |
| | 1 | 439 | | var thread = new Thread(() => |
| | 1 | 440 | | { |
| | 1 | 441 | | // Thread specific try\catch. |
| | 1 | 442 | | try |
| | 1 | 443 | | { |
| | 1 | 444 | | returnValue = target(arg1, arg2, arg3, arg4); |
| | 1 | 445 | | } |
| | 0 | 446 | | catch (Exception ex) |
| | 1 | 447 | | { |
| | 0 | 448 | | exception = ex; |
| | 0 | 449 | | } |
| | 2 | 450 | | }); |
| | | 451 | | |
| | | 452 | | // Important! Set thread apartment state to STA. |
| | 1 | 453 | | thread.SetApartmentState(System.Threading.ApartmentState.STA); |
| | | 454 | | |
| | | 455 | | // Start the thread. |
| | 1 | 456 | | thread.Start(); |
| | | 457 | | |
| | | 458 | | // Wait for the thread to finish. |
| | 1 | 459 | | thread.Join(); |
| | | 460 | | |
| | 1 | 461 | | return exception != null |
| | 1 | 462 | | ? throw new Exception("An unhandled exception has occurred. See inner exception for details.", exception) |
| | 1 | 463 | | : returnValue; |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | /// <summary> |
| | | 467 | | /// Solid Edge Application property. |
| | | 468 | | /// </summary> |
| | | 469 | | public Application Application |
| | | 470 | | { |
| | 1 | 471 | | get; |
| | 1 | 472 | | set => field = UnwrapRuntimeCallableWrapper<Application>(value); |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// Solid Edge Application property. |
| | | 477 | | /// </summary> |
| | | 478 | | public SolidEdgeDocument Document |
| | | 479 | | { |
| | 1 | 480 | | get; |
| | 1 | 481 | | 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> |
| | 4 | 490 | | protected static TInterface UnwrapRuntimeCallableWrapper<TInterface>(object rcw) where TInterface : class => rcw as |
| | | 491 | | } |