title: Release COM Object in C#
body: <p>I know this has been discussed earlier but I couldn't find a satisfactory answer. </p>
  
  <p>I have an e-mail file (.msg) which I open like below and then call Display.</p>
  
  <pre><code>oApp = new Microsoft.Office.Interop.Outlook.Application();
  mail = (Microsoft.Office.Interop.Outlook.MailItem)oApp.Session.OpenSharedItem(fileName);
  mail.Display(false);
  oApp = null; // do I need to release this object ?
  </code></pre>
  
  <p>users can close it and re-open it. Before they click "reopen" I check to see if the window still exists, if yes .. then just send a SetForeground(hwnd) to that window. if Not, that means user closed it so just release the mailItem object and open again.</p>
  
  <pre><code> public static void ReleaseCOMObject(Microsoft.Office.Interop.Outlook.MailItem item) {
          int r = System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
          while (r != 0) {
              r = System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
          }
      }
  </code></pre>
  
  <p>If I try to open the same file again. It "sometimes" throws a "file in use exception" So, I think even though I am releasing the MailItem .. its not been released properly.</p>
  
  <p>What can I do to ensure that its released properly. closing and reopening a file is a very common scenario. </p>
  
  <p>Any pointers will be very helpful.</p>