views:

79

answers:

1

My Outlook 2007 Addin works very well, until the point where I have to move mass amounts of messages. The list of mail items is obtained like this

Items itemObj = lNamespace.GetFolderFromID(Settings.Default.InputFolder).Items;

        List<MailItem> totalMailItems = new List<MailItem>();

        foreach (MailItem mailItem in itemObj)
        {
            totalMailItems.Add(mailItem);
        }

        //Dispose of itemObj
        itemObj = null;


            MAPIFolder blueFold = lNamespace.GetFolderFromID(Settings.Default.BlueFolder);
        MAPIFolder greenFold = lNamespace.GetFolderFromID(Settings.Default.GreenFolder);
        MAPIFolder orangeFold = lNamespace.GetFolderFromID(Settings.Default.OrangeFolder);
        MAPIFolder redFold = lNamespace.GetFolderFromID(Settings.Default.RedFolder);


    foreach (MailItem mailItem in totalMailItems)
        {
            MailItem xMail = mailItem;
            MessageRelevance mRel = new MessageRelevance();
            mRel = Process_MailItem(ref xMail);              

            xMail.Save();
            switch(mRel)
            {
                case MessageRelevance.Red:
                    xMail.Move(redFold);
                    _lvl2++;
                    break;
                case MessageRelevance.Orange:
                    xMail.Move(orangeFold);
                    _lvl1++;
                    break;
                case MessageRelevance.Blue:
                    xMail.Move(blueFold);
                    _nullLev++;
                    break;
                case MessageRelevance.Green:
                    xMail.Move(greenFold);
                    _lvl0++;
                    break;
            }            

            xMail = null;
        }

The reason I set xMail to mailItem is because I can't use mailItem as a reference, it's readonly. The rest of the program works great now, I'm just trying to figure out how to move these items faster. Do I have to call Save before? Or is that just an extra call?

A: 

I am not sure how many mail items you are actually moving, so I will assume alot.

The one thing that may be the issue is that the Save() method could be the bottleneck in your code. I had a similar type issue with an excel add-in that copied files to several locations. The solution to improve the speed and keep excel responsive was to use Asynchronous Delegate Invocation as described in Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads.

So in your example I would wrap the contents of the loop on totalMailItems. Note the code below may not be 100% correct but I hope its enough of an idea and guidance to help you out.

private delegate void SaveEmail(MailItem mailItem);

foreach (MailItem mailItem in totalMailItems)
{
    SaveEmail save = SaveMailItem; 
    IAsyncResult saveResult = save.BeginInvoke(mailItem, SaveCallBack, "MailItem Saved") 
    xMail = null;
}

private void SaveCallBack(IAsyncResult result)
{ // do stuff here if you want to... }

private void SaveMailItem(MailItem mailItem)
{
    MailItem xMail = mailItem;
    MessageRelevance mRel = new MessageRelevance();
    mRel = Process_MailItem(ref xMail);              

    xMail.Save();
    switch(mRel)
    {
        case MessageRelevance.Red:
            xMail.Move(redFold);
            _lvl2++;
            break;
        case MessageRelevance.Orange:
            xMail.Move(orangeFold);
            _lvl1++;
            break;
        case MessageRelevance.Blue:
            xMail.Move(blueFold);
            _nullLev++;
            break;
        case MessageRelevance.Green:
            xMail.Move(greenFold);
            _lvl0++;
            break;
    } 
}
Ahmad