Outlook plugin c# - Thread keeps outlook.exe process in Task Manag

T

TrinityPete

Hi,

I have written an outlook plugin that basically loads up lots of Contacts
from an ERP system. This works fine appart from the fact that it locks the
outlook UI. Therefore, I decided to spin up another thread to perform the
update.

Heres the odd thing - Even through debugger, I can see that the thread I
spin up completes is work and then the thread.IsAlive returns false but then
for some reason after exiting the outlook application. Outlook.exe still
appears in the task manager and the plugin dll is locked as it is in use by
another process. Kill the outlook .exe process and the lock on the dll is
removed. This suggests that the thread is not cleaning up properly, and there
must be some remnants of the thread remaining. I have tried thread.abort()
etc etc but this process still lies around until it is manually killed. My
best guess is that there is something odd happening with the com interop but
I havent a clue what........

Anyone got any ideas? They will all be greatfully received.

Thanks. Pete.
 
T

TrinityPete

Thanks for the info Ken,

I have tried using the backgroundworker class instead but still get the same
result - outlook process remains in task manager process tree after it has
been shut down. I decided to break down what was happening in the thread and
can now tie the issue down to a single line of code that causes the issue.
See below:

The worker is setup as follows:

workerthread = new BackgroundWorker();
workerthread.WorkerSupportsCancellation = true;
workerthread.DoWork += new DoWorkEventHandler(SynchroniseOutlook);
workerthread.RunWorkerAsync();


Example that works fine:
private void SynchroniseOutlook(object sender, DoWorkEventArgs e)
{
int a = 0;
}

Example that causes the process to hang around:

private void SynchroniseOutlook(object sender, DoWorkEventArgs e)
{
Microsoft.Office.Interop.Outlook.MAPIFolder defaultcontactfolder
= null;
try
{

_applicationObject.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
}
finally
{
Marshal.ReleaseComObject(defaultcontactfolder);
defaultcontactfolder = null;
}
}

thats all it seems to need to get the process to linger about in task
manager?????

Any ideas?

Pete
 
K

Ken Slovak - [MVP - Outlook]

Are you removing the event handler when you dispose of things?

What about if you explicitly call the garbage collector and wait for it to
finish when you release any objects?
 
T

TrinityPete

Hi Ken,

Once again, thanks for the response.

I managed to refactor using the Backgroundworker, getting the reference to
the mapi folders prior to spinning up the thread, then passing the Mapi
folders that I am interested in as parameters to the workerthread method.
This seems to have worked OK, outlook closes down correctly. As to why, I
have no idea why this should make that much difference.

Kind regards,
Pete.
 

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