1

次の問題は私を夢中にさせています。C#では、すべてのカレンダーイベントを取得するためのコードを実行した後、Outlookが確実にシャットダウンするようにしていますが、何を試しても、そうではありません。誰か助けてもらえますか?

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;
}
4

2 に答える 2

0

コードをデバッグして、アプリケーションが OutlookApp.Quit() コマンドに到達するかどうかを確認しましたか?

これを回避する唯一の方法は、プロセス "Outlook.exe" を無愛想に強制終了することです。

于 2012-04-12T16:12:31.617 に答える
0

一時的なハードコア ソリューション:

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();
    }
}
于 2012-04-12T19:06:01.350 に答える