首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Outlook Interop.Outlook未关闭

C# Outlook Interop.Outlook未关闭
EN

Stack Overflow用户
提问于 2012-04-12 23:52:53
回答 2查看 1.7K关注 0票数 1

下面的问题快把我逼疯了。在C#中,我试图确保outlook在我运行一些代码来获取所有日历事件后关闭,但无论我尝试什么,它都不能。有人能帮助我吗?

代码语言:javascript
复制
private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Outlook.Application outlookApp = null;
    Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null;
    Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null;

    outlookApp = new Microsoft.Office.Interop.Outlook.Application();
    mapiNamespace = outlookApp.GetNamespace("MAPI");

    CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
    outlookCalendarItems = CalendarFolder.Items;

    outlookCalendarItems.IncludeRecurrences = true;
    foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
    {
        if (item.IsRecurring)
        {
            Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
            DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0);
            DateTime last = new DateTime(2008, 10, 1);
            Microsoft.Office.Interop.Outlook.AppointmentItem recur = null;

            for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
            {
                try
                {
                    recur = rp.GetOccurrence(cur);
                    MessageBox.Show(recur.Subject + " -> " + cur.ToLongDateString());
                }
                catch
                {
                }
            }
        }
        else
        {
            MessageBox.Show(item.Subject + " -> " + item.Start.ToLongDateString());
            break;
        }
    }

    ((Microsoft.Office.Interop.Outlook._Application)outlookApp).Quit();
    //outlookApp.Quit();
    //(outlookApp as Microsoft.Office.Interop.Outlook._Application).Quit();

    outlookApp = null;

    System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookCalendarItems);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(CalendarFolder);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mapiNamespace);
    //System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookApp);

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookCalendarItems);
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(CalendarFolder);

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(mapiNamespace);
    //System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookApp);
    GC.Collect();
    GC.WaitForPendingFinalizers();

    mapiNamespace = null;
    CalendarFolder = null;
    outlookCalendarItems = null;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-13 03:06:01

临时核心解决方案:

代码语言:javascript
复制
public static class OutlookKiller
{
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
    private static extern long GetWindowThreadProcessId(long hWnd, out long lpdwProcessId);

    public static void Kill(ref Microsoft.Office.Interop.Outlook.Application app)
    {
        long processId = 0;
        long appHwnd = (long)app.Hwnd;

        GetWindowThreadProcessId(appHwnd, out processId);

        Process prc = Process.GetProcessById((int)processId);
        prc.Kill();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-04-13 00:12:32

您是否对代码进行了调试,以查看应用程序是否使用过OutlookApp.Quit()命令?

解决这个问题的唯一方法就是粗暴地杀死进程"Outlook.exe“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10127219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档