none
Finalizer doesn't run when Ctrl-C'ing an application. What's the alternative? RRS feed

  • Question

  • Hi!

    I've got a piece of code that has to get executed even if hte user stops my program using Ctrc-C (an external process has to be shut down).

    Is something like this possible? Even if I put the code into a disposable object, Dispose doesn't get called. Is this something for a critical finalizer? Unfortunately I've never understood how to use them properly. Maybe someone can help me there? Here's the Disposable object:

     

    class DxDContainer : IDisposable
    	{
    	public DxDContainer(FileInfo DxDPrjFile, bool Silent, bool DoNothing)
    		{
    		Application = null;
    		Application = (ViewDraw.Application)Activator.CreateInstance(Type.GetTypeFromProgID("Viewdraw.Application"));
    		Application.SilentMode = ViewDraw.VdSilentMode.VDSM_ALL;
    		Application.Initialize(System.Environment.GetEnvironmentVariable("WDIR"), System.Environment.GetEnvironmentVariable("SDD_LM_BIN"));
    		Application.OpenProject(DxDPrjFile.FullName);
    		Application.Visible = false;
    		}
    	public ViewDraw.Application Application { get; private set; }
    	bool _disposed = false;
    	public void Dispose()
    		{
    		Dispose(true);
    		}
    	protected virtual void Dispose(bool disposing)
    		{
    		_disposed = true;
    		if (!_disposed)
    			{
    			if (disposing)
    				{
    				//clean up managed objects
    				}
    			// clean up unmanaged objects
    			if (Application != null)
    				{
    				Application.Quit();
    				Application = null;
    				}
    			GC.SuppressFinalize(this);
    			}
    		}
    	~DxDContainer()
    		{
    		Dispose(false);
    		GC.KeepAlive(Application);
    		}
    	}
    

     

    Is there a way I can make the finalizer run even when someone presses Ctrl-C?

     

    Lots of Greetings!

    Volker


    Volker
    • Edited by Hetzi Tuesday, January 31, 2012 10:40 AM
    Tuesday, January 31, 2012 10:35 AM

Answers

  • Hi,

    the big problem is, that nothing is executed, when the process is killed by the user.

    I understood that you have a console application in which the user stops the program through Ctrl-C. You can prevent that the process is killed. Maybe this is a solution for you:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682541(v=vs.85).aspx

    But please be aware: The user can still kill the process so the problem is not generaly solved (where I see no solution!). Only Ctrl-C / Ctrl-Break are now handled by your application.

    With kind regards,

    Konrad

    • Marked as answer by Hetzi Tuesday, January 31, 2012 12:04 PM
    Tuesday, January 31, 2012 10:48 AM

All replies

  • Hi,

    the big problem is, that nothing is executed, when the process is killed by the user.

    I understood that you have a console application in which the user stops the program through Ctrl-C. You can prevent that the process is killed. Maybe this is a solution for you:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682541(v=vs.85).aspx

    But please be aware: The user can still kill the process so the problem is not generaly solved (where I see no solution!). Only Ctrl-C / Ctrl-Break are now handled by your application.

    With kind regards,

    Konrad

    • Marked as answer by Hetzi Tuesday, January 31, 2012 12:04 PM
    Tuesday, January 31, 2012 10:48 AM
  • Ohh ... another thing I just saw:

      _disposed = true;
      if (!_disposed)

    The code inside the if will never be executed. Maybe you want to have a closer look at different articles about IDisposable / Dispose Pattern. CodeProject has plenty articles on this topic.

    With kind regards,

    Konrad

    Tuesday, January 31, 2012 10:50 AM
  • Ok, so there's no solution for the general case.

    Bad luck.


    Volker
    Tuesday, January 31, 2012 12:05 PM
  • Fixed that. Thanks for the hint!

    Volker


    Volker
    Tuesday, January 31, 2012 12:05 PM