Beantwortet Outlook 2007 ItemChange Event

  • Mittwoch, 20. August 2008 13:55
     
     
    Hey!

    I´m currently writing an Outlook 2007 Addin and I´m using the ItemChange Event on the Calendar Folder. But the following code runs in a
    infinite loop. Please help me.

    void StartUp()
            {
                try
                {
                    oNS = Application.GetNamespace("mapi");
                    oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                    items = null;

                    items = oCalendar.Items;
                    items.ItemChange += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(items_ItemChange);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
           
            void items_ItemChange(object Item)
            {
                try
                {
                    if (isChange == 0)
                    {
                        Outlook.AppointmentItem current = null;

                        try
                        {
                            current = (Outlook.AppointmentItem)Item;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                        if (current != null)
                        {
                            current.Location = "Test";
                            current.Save();

                            isChange++;
                        }
                    }
                    else
                    {
                        isChange = 0;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

Alle Antworten

  • Mittwoch, 20. August 2008 22:15
    Moderator
     
     Beantwortet

    This took a while for me to see the problem but is obvious Wink

     

    When you current.Save() ----> it change's the item and fires the event again hence the loop

     

    Regards

     

     

  • Donnerstag, 21. August 2008 11:39
     
     
    For that reason I´ve implemented the isChange Flag that does not change the Item in the second cycle.

    I don´t think that the problem is obvious!

    Regards
  • Montag, 25. August 2008 14:59
     
     
    Maybe you should change lines

    current.Save();
    isChange++;


    to

    isChange++;
    current.Save();


    Regards,
    Robert
  • Mittwoch, 3. September 2008 13:07
     
     
    I tried this change but the event fires still in a infinite loop. I can´t understand it, please help me.
  • Mittwoch, 3. September 2008 13:12
     
     

    Try moving isChange++; even more to the top:

    isChange++;

    current.Location = "Test";
    current.Save();

    or at the start of the function.

     

  • Freitag, 5. September 2008 18:42
     
     

     

    why don't you unhook the event handler from "void items_ItemChange(object Item)" and hook -it back once you are done....

    for.eg.

           

    void items_ItemChange(object Item)

    {

    items.ItemChange -= ItemChangeHandler;

    //.........................do whaterver you want..

    finally

    {

    items.ItemChange += ItemChangeHandler;

    }

    }

  • Donnerstag, 26. Juli 2012 05:06
     
     
    The question is how to determine that moment (I mean when no more ItemChange calls are expected)
  • Freitag, 27. Juli 2012 21:55
     
     

    The event will be fired asynchrnously, so that moment cannot be determined.

    If your code has already made the modification, can you check if the modification needs to be done again? In the code above the Location property does not need to be set and Save does not need to be called if the property is already set to the expected value:

    if (current.Location != "Test")
    {
         current.Location = "Test";
         current.Save();
    }


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

  • Samstag, 28. Juli 2012 06:15
     
     
    Nope, doesn't work in my case because my code was supposed to replicate incoming appointments via reading MSSQL db, and each appointment may has dozens of changes in all its fields. So I don't think the direct comparison would work in my case.
  • Montag, 30. Juli 2012 07:31
     
     
    then maybe check LastModificationTime and see if you have lastest data in your db
  • Montag, 30. Juli 2012 07:43
     
     

    There's one more complication - the SQL server and the Outlook machine will definitely have different time settings. Time synchronization is absent, and I cant count on its presence - this is how my client wants to see it.

    Ideally I wish Outlook had an opportunity of switching to synchronous event processing by plugin's demand and switching back by demand again :)

     
    • Bearbeitet Senglory Montag, 30. Juli 2012 07:45
    •  
  • Montag, 30. Juli 2012 07:52
     
     
    no time synchronization - store lastmodificationtime along with appointment data in db and later check against that value to determine if you have 'stale' data.