开发者

C#: foreach loop to for loop COM-DotNet programming leaky Outlook items

all. I'm having problem running my Outlook 2010 Add-ins in none-cached Exchange mode. It seem like there is a limit on how many calls you can make to Exchange. From this article my co-workers believe if we take care of all the leaky problem then there shouldn't be any problem. Thus, I have a following function that return IEnumerable that was written with foreach 开发者_Python百科loop. I tried to converted it to for loop but I can't get all the folders that I want no matter how deep it goes and it is also weird that I need to start the count with 1 or else I get an Index out of bound error. Can someone take a look at my for loop and tell me why it doesn't get all child folders from root folder? For example with foreach loop I can get folderA, folderB and folderC with all the child folders in folderC but with for loop I can only get folderA, folderB and folderC.

Foreach loop:

private static IEnumerable<Outlook.Folder> getChildFolders(Outlook.Folder rootFolder, string folderToSkip)
    {
        foreach (Outlook.Folder t in rootFolder.Folders)
        {
            if (t.Name.Equals(folderToSkip) && !String.IsNullOrEmpty(folderToSkip))
            {
                continue;
            }
            yield return t;
            foreach (Outlook.Folder i in getChildFolders(t, folderToSkip)) { yield return i; }
        }           
    }

For loop:

for (int count = 1; rootFolder.Folders.Count > count; count++) { if (rootFolder.Folders[count].Name.Equals(folderToSkip) && !String.IsNullOrEmpty(folderToSkip)) { continue; } var t = rootFolder.Folders[count] as Outlook.Folder; yield return t;

            for (int i = 1; t.Folders.Count > i; i++)
            {
                var f = getChildFolders(t, folderToSkip).OfType<Outlook.Folder>().ToList()[i] as Outlook.Folder;
                yield return f;
            }
        }                  

I don't really think the foreach loop will cause any leaky problem in Outlook. I tried the second for loop like the above but I'm still not able to get it run correctly. I'm really struggling with this. Can someone show me what and where I'm getting it wrong?


Your for loop isn't getting the child folders because you forgot to implement the recursion which exists in the foreach loop. You'll enter the root level, and get all children of the root, but then it will stop there. You need to recursively interact with each child folder to drill all the way down.

Edit to add:

Note this line foreach (Outlook.Folder i in getChildFolders(t, folderToSkip)) { yield return i; }

This is the recursion you're missing.


You are right about the leaking, you need to be careful doing any sort of for loop.

What are you trying to do, why do you need a reference to all folders?

If you must, this is how you do it without leaking.

private static IEnumerable<Outlook.Folder> getChildFolders(Outlook.Folder rootFolder, string folderToSkip)
{
    FolderCollection folders;
    try
    {
        folders = rootFolder.Folders
        foreach (Outlook.Folder t in folders)
        {
            if (t.Name.Equals(folderToSkip) && !String.IsNullOrEmpty(folderToSkip))
                continue;

            yield return t;
            foreach (Outlook.Folder i in getChildFolders(t, folderToSkip)) { yield return i; }
        }
    }
    finally
    {
        if (folders != null && Marshal.IsComObject(folders))
            Marshal.ReleaseComObject(folders);
    }        
}

Then you have to call Marshal.ReleaseComObject on all the folders that have been yeilded from that method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜