المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : premature garbage collection of static class causing problem



C# Programming
09-18-2009, 04:13 AM
Well I know only objects are garbage collected, but please hear me out..

I have a class "serverClass" in my program.cs file in a project. All the members of this class are static, moreover the members of this class are DLLIMPORTS of an unmanaged Dll. This file also has the IDE generated "Program" class which has the Main() function that invokes the Form1 GUI i.e.; Application.Run(new Form1());

Now, I have the Form1.cs file with Form1 class that has buttons and listboxes. The Form1 class has the button events, etc, i have also created a couple of member functions in the Form1 class (startServer() and stopServer()). These functions call the static members of the "serverClass" from the program.cs file. But as I said earlier the serverClass is static class and hence I cannot create an instance to access its functions, instead I go serverClass.function1() (i.e.; className.memberFunction()), serverClass.function2(), etc.

I want to access the startServer() and stopServer() functions on the click of the buttons "Start" and "Stop" respectively. So in the click events of these buttons I call these functions.

The project compiles fine. When I run the application/form and click 'Start' button the startServer() function is called and the server runs fine as desired. But my problem is when I click the 'Stop' button and the stopServer() function is called, I get a runtime error "CallbackOnCollectedDelegate" on the line in the stopServer() function where I call a member of serverClass as serverClass.function2().

I might be wrong in the class structuring itself....I appreciate all the suggestions and ideas.

BTW all of the mentioned classes are in the same namespace.