Outlook 2007 / Vista

T

Tapper

I am trying to write an application in C# (VS2005) that does something when
a contact is changed. It appears that I am having some sort of threading
issue in that when the event is fired, the handler is off in another thread
from my application. Here's some code that is an example where a listbox
entry should be added when a contact is changed; the event does indeed fire
but the listbox does not get changed.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Office.Interop.Outlook;

namespace TestOutlook
{
class OutlookAppClass2
{

private ApplicationClass objOutlook;
private Microsoft.Office.Interop.Outlook.NameSpace nameSpace = null;
private Microsoft.Office.Interop.Outlook.MAPIFolder fldContacts =
null;
private Microsoft.Office.Interop.Outlook.Items contactItems = null;
private System.Windows.Forms.ListBox passedListBox;

internal OutlookAppClass2()
{
}

internal void InitializeOutlook(ref System.Windows.Forms.ListBox
listBox)
{
passedListBox = listBox;
passedListBox.Items.Add("Start of Initalizing Outlook Class -
thread = " + System.Threading.Thread.CurrentThread.Name);
objOutlook = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
nameSpace = objOutlook.GetNamespace("MAPI");
fldContacts =
nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
contactItems = fldContacts.Items;
contactItems.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(contactItems_ItemChange);
passedListBox.Items.Add("End of Initalizing Outlook Class -
thread = " + System.Threading.Thread.CurrentThread.Name);
}

private void contactItems_ItemChange(object thisItem)
{
string threadName = System.Threading.Thread.CurrentThread.Name;
passedListBox.Items.Add("Contact Change Event - thread = " +
System.Threading.Thread.CurrentThread.Name);
}


internal void CloseOutlook()
{
try
{
nameSpace.Logoff();
}
catch (System.Exception ex)
{
passedListBox.Items.Add("NameSpaceLogoff Exception: " +
ex.ToString());
}
if (fldContacts != null)
ReallyClear((object)fldContacts);
if (objOutlook != null)
{
System.Threading.Thread.Sleep(200);
ReallyClear(objOutlook);
}
}


private void ReallyClear(object thisObj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(thisObj);
}
catch (System.Exception ex)
{
passedListBox.Items.Add("ReallyClear Exception: " +
ex.ToString());
}
thisObj = null;
}



}


public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private ListBox listBox1;
private OutlookAppClass2 outlookObj;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
System.Threading.Thread.CurrentThread.Name = "Main Thread";
outlookObj = new OutlookAppClass2();
outlookObj.InitializeOutlook(ref listBox1);
//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
outlookObj.CloseOutlook();
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(1, 10);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(259, 251);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(263, 264);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top