microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f46fff4e5103217703b51e27ba3f6405ac000e21

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/email/COMObject.cs

61lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Runtime.InteropServices;
5
6namespace TypeAgent;
7
8public class COMObject : IDisposable
9{
10 bool _disposed;
11
12 public COMObject()
13 {
14 }
15
16 ~COMObject()
17 {
18 Dispose(false);
19 }
20
21 public void Dispose()
22 {
23 Dispose(true);
24 GC.SuppressFinalize(this);
25 }
26
27#pragma warning disable CA1063
28 void Dispose(bool fromDispose)
29 {
30 if (!_disposed)
31 {
32 OnDispose();
33 _disposed = true;
34 }
35 }
36
37 protected virtual void OnDispose() {}
38
39 public static void Release(object value)
40 {
41#pragma warning disable CA1416
42 if (value != null)
43 {
44 Marshal.ReleaseComObject(value);
45 }
46 }
47
48 public static void Release(IEnumerable<object> values)
49 {
50 foreach (object value in values)
51 {
52 Release(value);
53 }
54 }
55
56 public static void ReleaseAll()
57 {
58 GC.Collect();
59 GC.WaitForFullGCComplete();
60 }
61}