I released all the references and removed all the events added as well but Outlook.exe still
won't quit even after being told to go away. I'm confused. The code with changes is below:
private Outlook.Application m_oApp = null;
private Outlook.MailItem m_oMail = null;
private Outlook.Inspectors m_oInsp = null;
private Outlook.InspectorEvents_Event m_oInspector = null;
private void button_Click(object sender, System.EventArgs e)
{
try
{
m_oApp = new Outlook.Application();
oApp.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler( Outlook_ItemSent);
m_oInsp = m_oApp.Inspectors;
m_oInsp.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
m_oMail = (Outlook.MailItem)m_oApp.CreateItem(Outlook.OlItem Type.olMailItem);
m_oMail.Display(m_oApp);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
private void Outlook_ItemSent(object Item, ref bool Cancel)
{
Debug.WriteLine("Outlook_ItemSent");
m_oMail.Send(); // Force to send email now
}
private void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Debug.WriteLine("Inspectors_NewInspector");
m_oInspector = (Outlook.InspectorEvents_Event)Inspector;
m_oInspector.Close += new Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
}
private void Outlook_Closed()
{
Debug.WriteLine("Outlook_Closed");
try
{
//m_oMail.Close(Outlook.OlInspectorClose.olPromptFor Save);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oMail);
GC.Collect();
GC.WaitForPendingFinalizers();
m_oInspector.Close -= new Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oInspector);
GC.Collect();
GC.WaitForPendingFinalizers();
m_oInsp.NewInspector -= new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oInsp);
GC.Collect();
GC.WaitForPendingFinalizers();
m_oApp.ItemSend -= new Outlook.ApplicationEvents_11_ItemSendEventHandler( Outlook_ItemSent);
m_oApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
m_oMail = null;
m_oInspector = null;
m_oInsp = null;
m_oApp = null;
Debug.WriteLine("Cleaned up everything....");
}
}
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:up*************@tk2msftngp13.phx.gbl...
Max,
When you go that road, you have to release the Inspector event reference
too... (a COM connection point object)
Outlook.InspectorEvents_Event Inspect =
(Outlook.InspectorEvents_Event)Inspector;
..
// when done release all COM references when you need deterministic release
of the COM server.
Marshal.ReleaseComObject(Inspect);
Marshal.ReleaseComObject(..);
..
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers()
Willy.
"Max" <ma******@yahoo.com> wrote in message
news:uh**************@TK2MSFTNGP14.phx.gbl...I have some code that launches MS Outlook and creates a new email message
then
gives control of the Outlook application to the user by displaying it on
the screen.
The problem is that when the user closes the Outlook application or sends
an email
the Outlook window closes but the process Outlook.exe remains in memory. I
have my
code waiting for the close event from the COM and I call Quit() on the
Outlook
application but Outlook.exe still hangs in memory. Can anyone tell me what
I'm doing
wrong? I just want to get Outlook.exe out of the memory. Sample code is
below:
private Outlook.Application m_oApp = null;
private Outlook.MailItem m_oMail = null;
private void button_Click(object sender, System.EventArgs e)
{
try
{
m_oApp = new Outlook.Application();
m_oApp.Inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
m_oMail =
(Outlook.MailItem)m_oApp.CreateItem(Outlook.OlItem Type.olMailItem);
m_oMail.Display(m_oApp);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
private void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Debug.WriteLine("Inspectors_NewInspector");
((Outlook.InspectorEvents_Event)Inspector).Close += new
Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
}
private void Outlook_Closed()
{
Debug.WriteLine("Outlook_Closed");
try
{
//m_oMail.Close(Outlook.OlInspectorClose.olPromptFor Save);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oMail);
GC.Collect();
GC.WaitForPendingFinalizers();
m_oApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(m_oApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
m_oMail = null;
m_oApp = null;
Debug.WriteLine("Cleaned up everything....");
}
}