none
WORD 2010 ADDIN - SHUTDOWN, DISPATCHER AND GARBAGE COLLECTION RRS feed

  • Question

  • Hellooo

    Wondered if anyone could help and shed some light on a current issue we have.  Hopefully I will explain it well.

    Within our Addin - we are having to intercept the open and perform a series of tasks that also involves a timer (hate this as I see it messing around a little too much with the open event - however this is whats required to provide the ability to automate some functionality)

    Given a timer is in the mix and users can open several documents quickly - we have to pass in the parent to messagebox that gets displayed to ensure the message is attached to the correct document:

    IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp", sCaption+ " - Microsoft Word");
    NativeWindow nativeWindow = new NativeWindow();
    nativeWindow.AssignHandle(wordWindow);

    (we pass nativewindow into messagebox.showdialog)

    Since we have added this code, we are now experiencing the following error on the addin being closed:

    System.AppDomainUnloadedException was unhandled
    Message: The application domain in which the thread was running has been unloaded.

    I have seen blogs stating that you should make a call to Dispatcher.CurrentDispatcher.InvokeShutdown(); on shutdown, but this doesnt always work which I kinda yunderstand because threading is involved with the timer.

    Can anyone shed anylight on this - Im reluctant to put out the new build until I feel comfortable I havent got any leaks going on and its cleaning shutting down.

    Hope you can help - I have placed some sample code below:

    anna

    sample code:

    void Application_DocumentOpen(Word.Document Doc)
    {
    try
    {
        //do some stuff
        OpenWordDocument(Doc, sFileName, sTemplateName, bComments);

       // more stuff
    }
    catch{}
    finally{}

    public void OpenWordDocument(Word.Document Doc, String sFileName, String sTemplateName, Boolean bComments)
    {
    oDoc = Doc;
    oFileName = sFileName;
    oTemplateName = sTemplateName;
    oComments = bComments;
    _convertTimer = new System.Timers.Timer(50);
    _convertTimer.Elapsed += new System.Timers.ElapsedEventHandler(_OpenDocumentLapsed);
    _convertTimer.AutoReset = false;
    _convertTimer.Enabled = true;
    }

    private void _OpenDocumentLapsed(object sender, System.Timers.ElapsedEventArgs e)

    {

    try
    {
        //do some stuff
       }
    catch{}
    finally{}

    }

    // error occurs when debugging on GC.Collect below

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

    {
    GC.Collect();
    GC.WaitForPendingFinalizers();

    }

               


    Anna

    Friday, March 23, 2012 3:38 PM

Answers

All replies