473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Handle COM events

Max
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....");
}
}

Nov 17 '05 #1
5 4660
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....");
}
}

Nov 17 '05 #2
Max

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....");
}
}


Nov 17 '05 #3

"Max" <ma******@yahoo.com> wrote in message
news:eY*************@TK2MSFTNGP15.phx.gbl...

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:


Well, I changed your code a bit and transformed it into a console app.
Try this and watch Outlook.exe in taskman.... (this works for me)

using System;
using System.Runtime.InteropServices;
using Outlook; // your PIA namespace
class Tester
{
static Application oApp;
static MailItem myMail;
static Outlook.InspectorEvents_Event Inspect;
[STAThread]
static void Main()
{
oApp = new Application();
oApp.Inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
myMail = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
myMail.Display(oApp);
myMail.Recipients.Add("nnnnnn@xxxxxx");
myMail.Body = "Test";
// Wait 30 seconds before terminating this client application.
// keep pumping messages (for STA threads only), don't use Sleep in STA
threads!!!) so the finalizer can run.
System.Threading.Thread.CurrentThread.Join(30000);
}
static void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Console.WriteLine("Inspectors_NewInspector");
Inspect = (Outlook.InspectorEvents_Event)Inspector;
Inspect.Close +=
new Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
}

static void Outlook_Closed()
{
Console.WriteLine("Outlook_Closed");
try
{
((Outlook._Application)oApp).Quit();
Marshal.ReleaseComObject(Inspect);
Marshal.ReleaseComObject(myMail);
Marshal.ReleaseComObject(oApp);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
Console.WriteLine("Outlook.exe should terminate real soon....");
}
}
}
Willy.
Nov 17 '05 #4
Max
Willy thanks so much for your help! I copied your code and tried it out but
Outlook.exe still remains in memory :( Is there a way to debug this and see
what is holding it from quitting, ie any references not closed.... ?
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...

"Max" <ma******@yahoo.com> wrote in message
news:eY*************@TK2MSFTNGP15.phx.gbl...

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:


Well, I changed your code a bit and transformed it into a console app.
Try this and watch Outlook.exe in taskman.... (this works for me)

using System;
using System.Runtime.InteropServices;
using Outlook; // your PIA namespace
class Tester
{
static Application oApp;
static MailItem myMail;
static Outlook.InspectorEvents_Event Inspect;
[STAThread]
static void Main()
{
oApp = new Application();
oApp.Inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
myMail = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
myMail.Display(oApp);
myMail.Recipients.Add("nnnnnn@xxxxxx");
myMail.Body = "Test";
// Wait 30 seconds before terminating this client application.
// keep pumping messages (for STA threads only), don't use Sleep in STA
threads!!!) so the finalizer can run.
System.Threading.Thread.CurrentThread.Join(30000);
}
static void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Console.WriteLine("Inspectors_NewInspector");
Inspect = (Outlook.InspectorEvents_Event)Inspector;
Inspect.Close +=
new Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
}

static void Outlook_Closed()
{
Console.WriteLine("Outlook_Closed");
try
{
((Outlook._Application)oApp).Quit();
Marshal.ReleaseComObject(Inspect);
Marshal.ReleaseComObject(myMail);
Marshal.ReleaseComObject(oApp);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
Console.WriteLine("Outlook.exe should terminate real soon....");
}
}
}
Willy.

Nov 17 '05 #5
Max
I just tested your code again on another computer and it seems that Outlook.exe
is terminated correctly there. I'm not sure now why it does not terminate on my
laptop which I use for development. I have dissabled some services and I also have
antivirus and firewal software along with other anti-spyware running in the backgroud.
Is there a particular service responsible for the removal of Outlook.exe which I might
have dissabled and I'm not aware of?

Cheers,
Max
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...

"Max" <ma******@yahoo.com> wrote in message
news:eY*************@TK2MSFTNGP15.phx.gbl...

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:


Well, I changed your code a bit and transformed it into a console app.
Try this and watch Outlook.exe in taskman.... (this works for me)

using System;
using System.Runtime.InteropServices;
using Outlook; // your PIA namespace
class Tester
{
static Application oApp;
static MailItem myMail;
static Outlook.InspectorEvents_Event Inspect;
[STAThread]
static void Main()
{
oApp = new Application();
oApp.Inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_NewInspector);
myMail = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
myMail.Display(oApp);
myMail.Recipients.Add("nnnnnn@xxxxxx");
myMail.Body = "Test";
// Wait 30 seconds before terminating this client application.
// keep pumping messages (for STA threads only), don't use Sleep in STA
threads!!!) so the finalizer can run.
System.Threading.Thread.CurrentThread.Join(30000);
}
static void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Console.WriteLine("Inspectors_NewInspector");
Inspect = (Outlook.InspectorEvents_Event)Inspector;
Inspect.Close +=
new Outlook.InspectorEvents_CloseEventHandler(Outlook_ Closed);
}

static void Outlook_Closed()
{
Console.WriteLine("Outlook_Closed");
try
{
((Outlook._Application)oApp).Quit();
Marshal.ReleaseComObject(Inspect);
Marshal.ReleaseComObject(myMail);
Marshal.ReleaseComObject(oApp);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
finally
{
Console.WriteLine("Outlook.exe should terminate real soon....");
}
}
}
Willy.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: DraguVaso | last post by:
Hi, I have a Generic List, for instance: Public MyPersonnesDeContact As New System.Collections.Generic.List(Of clsPersonne) My clsPersonne raises some events, and I want to be able to handle...
0
by: lkr | last post by:
hi i got the code for handling some of the events in MSWord.But i need ScrollBar events to be handled .i got the handle to the word application window through an API using ForeGroundWindow().By...
7
by: lkr | last post by:
hi is there is anyway to capture the document events of a MSWord?eg:I have to handle the event awhen WM_KEYUP or anyother events will occur. give me a solution............ thanks in advance lkr
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
4
by: Paul Fi | last post by:
I want to handle external events from my c# app, especially to events from windows explorer (windows shell) and windows media player how do i go about handling such external events? *** Sent...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.