none
C# class library instances are not destrying after the execution in the VSTO addin RRS feed

  • Question

  • Hi,

    I have created a class library in c# so that I can use in Microsoft Word VSTO project. But I have a problem. 

    This is an exmaple for my class library. Just call it as TestClassLibrary

    public class Test{

         private static Test _instance;

         protected Test(){

               //constructor     

         }

         //Singeleton   pattern

        public static Test Instance(){

          if(_instance ==  null){

              _instance = new Test();   

          }

          return _instance;

         }

         public string Name{

               get;

               set;

         }

    }

    in my VSTO add-in 

     public void OnShowButton(Office.IRibbonControl control)
     {
                MessageBox.Show(TestClassLibrary.Test.Name);

                TestClassLibrary.Test.Name = "test add-in";

      }

    If I execute the OnShowButton event in the add-in, the first output is empty message box. But when I execut the event second time I got the message box as "test add-in". It should be empty message box. Its look like. So its look like library instance are not destroying in the add-in after execution. Does  any one have an idea about this? Or do I need to clear each instance?


    Wednesday, July 25, 2018 5:16 PM

All replies

  • Hello nishmj,

    It seems that the Test class is a Singleton class, right? I'm wondering how could the code work, the Name property is not static, so how could we call it as TestClassLibrary.Test.Name directly?

    Besides, I'm wondering if the issue is related to VSTO project. If you try to do the same test in a simple windows form project, will the issue still exist?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 26, 2018 9:52 AM
  • Hello Terry,

    Thanks for the reply. Yes the test calss is Singeleton. I have done some mistakes when I writing the OnShowButton event. Sorry for inconvenience. This is the correct one;

    public void OnShowButton(Office.IRibbonControl control)
     {
                MessageBox.Show(TestClassLibrary.Test.Instance().Name);

                TestClassLibrary.Test.Instance().Name = "test add-in";

      }

    So now it make sense for the problem you have asked "how could we call it as TestClassLibrary.Test.Name directly". It is TestClassLibrary.Test.Instance().Name

    I have already tried a windows form application as well. I got the same result. Looks like its crucial to clear objects after the execution. Isn't it?

    Friday, July 27, 2018 3:46 AM
  • I am thinking about to set  the _instance =null in the dispose method in the class Test above. So garbage collector will take care of underlying objects within _instance. What do you think about it?
    Friday, July 27, 2018 4:32 AM
  • Hello nishmj,

    This forum is for discussing developing issue related to Word/Word VSTO. However, based on your code, I think your issue is still more related to C# Singleton. I will move the thread back to Visual C# forum.

    Thanks for understanding,

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 27, 2018 6:05 AM
  • Thanks Terry. Looking forward to hear from someone.
    Friday, July 27, 2018 6:08 AM
  • Classes with static members are initialized just before the first usage of the class (.NET v4.5+ behavior) or when the library loads (pre-.NETv4.5 behavior) The static member would stay in memory until the process is terminated.

    To control the lifetime of types with static variable, you'd need to use your class library as proxy class that calls what you want to do, and then manually unload it when done.


    Friday, July 27, 2018 8:57 AM
    Answerer