none
Regarding callbackOnCollectedDelegate RRS feed

  • Question

  •  

    Hello Guys

     

    I am developing one utility which is having number of socket connections with the server. Now this is console application

    what I want to do is when ever I closed the application it should write detailed report which will contain some info like the time it is been started,it stoped etc the problem is not with this what happened when I exited from console it is giving me error something like this :" A callback was made on a garbage collected delegate of type 'Client Utility!Client_Utility.Program+HandlerRoutine::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."  

     

    My code is something like this:

     

    [DllImport("Kernel32")]

    public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);

    // A delegate type to be used as the handler routine

    // for SetConsoleCtrlHandler.

    public delegate bool HandlerRoutine(CtrlTypes CtrlType);

    // An enumerated type for the control messages

    // sent to the handler routine.

    public enum CtrlTypes

    {

    CTRL_C_EVENT = 0,

    CTRL_BREAK_EVENT,

    CTRL_CLOSE_EVENT,

    CTRL_LOGOFF_EVENT = 5,

    CTRL_SHUTDOWN_EVENT

    }

    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)

    {

    bool ok = true;

    switch (ctrlType)

    {

    case CtrlTypes.CTRL_CLOSE_EVENT:

    {

    Console.WriteLine("App");

    save_report("Application ending because of Close Event by user");

    ok = false;

    break;

    }

    case CtrlTypes.CTRL_BREAK_EVENT:

    {

    Console.WriteLine("App_break");

    Console.ReadLine();

    break;

    }

    case CtrlTypes.CTRL_C_EVENT:

    {

    Console.WriteLine("App_ctrl+c");

    Console.ReadLine();

    break;

    }

    case CtrlTypes.CTRL_LOGOFF_EVENT:

    {

    Console.WriteLine("App_logoff");

    Console.ReadLine();

    break;

    }

    case CtrlTypes.CTRL_SHUTDOWN_EVENT:

    {

    Console.WriteLine("App_shutdown");

    Console.ReadLine();

    break;

    }

    }

    // Put your own handler here

    return ok;

    }

    Please help out of this its urgent!

     

    thanks in advance

    Friday, March 7, 2008 6:34 AM

Answers

  • This problem when using SetConsoleCtrlHandler is rather common. The fix sometimes involves GC.KeepAlive.  In fact, the documentation for GC.KeepAlive includes an example that uses SetConsoleCtrlHandler.

     

    http://msdn2.microsoft.com/en-us/library/system.gc.keepalive.aspx

     

    In other cases, what you need to do is declare a static variable of the HandlerRoutine delegate type pointing at your method.  Since this is a static variable it will remain alive for the lifetime of your program.

     

    private static HandlerRoutine _ConsoleCtrlCheckDelegate = ConsoleCtrlCheck;

     

    Then, when you call SetConsoleCtrlHandler, pass the _ConsoleCtrlCheckDelegate as the handler parameter.

     

    Friday, March 7, 2008 11:49 PM

All replies

  • This problem when using SetConsoleCtrlHandler is rather common. The fix sometimes involves GC.KeepAlive.  In fact, the documentation for GC.KeepAlive includes an example that uses SetConsoleCtrlHandler.

     

    http://msdn2.microsoft.com/en-us/library/system.gc.keepalive.aspx

     

    In other cases, what you need to do is declare a static variable of the HandlerRoutine delegate type pointing at your method.  Since this is a static variable it will remain alive for the lifetime of your program.

     

    private static HandlerRoutine _ConsoleCtrlCheckDelegate = ConsoleCtrlCheck;

     

    Then, when you call SetConsoleCtrlHandler, pass the _ConsoleCtrlCheckDelegate as the handler parameter.

     

    Friday, March 7, 2008 11:49 PM
  • Thanks a Lot Brother

     

    Actually I visited the GC Class methods at MSDN.But that time was in very hurry neglected it. Really thanks keep it on

     

    thanks & Regards

    Swapnil

    Saturday, March 8, 2008 4:06 AM