Tech-Today
How to catch closing event of a console application in C#
On application closing we certainly always need to perform a clean up. But on the case of a c# console application there is no clear way to do it. And this is what I came up, to import a function from kernel32.dll, which is SetConsoleCtrlHandler.
I'll let my code explain the rest:
class Program
{
#region Page Event Setup
enum ConsoleCtrlHandlerCode : uint
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
delegate bool ConsoleCtrlHandlerDelegate(ConsoleCtrlHandlerCode eventCode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandlerDelegate handlerProc, bool add);
static ConsoleCtrlHandlerDelegate _consoleHandler;
#endregion
public Program()
{
_consoleHandler = new ConsoleCtrlHandlerDelegate(ConsoleEventHandler);
SetConsoleCtrlHandler(_consoleHandler, true);
}
#region Page Events
bool ConsoleEventHandler(ConsoleCtrlHandlerCode eventCode)
{
// Handle close event here...
switch (eventCode)
{
case ConsoleCtrlHandlerCode.CTRL_CLOSE_EVENT:
case ConsoleCtrlHandlerCode.CTRL_BREAK_EVENT:
case ConsoleCtrlHandlerCode.CTRL_LOGOFF_EVENT:
case ConsoleCtrlHandlerCode.CTRL_SHUTDOWN_EVENT:
Destroy();
Environment.Exit(0);
break;
}
return (false);
}
#endregion
}
-
How To Detect First Time App Launch On Iphone In Xcode
Inside the "AppDelegate.m", change - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } to - (BOOL)application:(UIApplication...
-
Application's First Launch Xcode
Open AppDelegate.swift and modify this line: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { return true } to func application(application: UIApplication, didFinishLaunchingWithOptions...
-
How To Change Jquery's Dialog Button Label At Run Time
Recently I've used a 3rd party jquery library that pops up a jquery dialog with form content. However if has a default button Save and Cancel, which most of the time is ok, but sometimes you have need to localized or change the label depending on...
-
Add A Jquery Datepicker On Jquery Dialog Box Via Load Method
Recently I've worked on a form that requires a jquery datepicker to be rendered inside jquery's dialog element. Where I encountered several problems like: 1.) datepicker() should be invoke inside dialog 2.) the dialog created (div) should be remove...
-
Dynamic And Or Statements In Entity Framework
What if your where clause is dynamic meaning certain fields only gets filtered when a certain condition is satisfied? The most simply implementation of AND, OR without condition is: entity.Model.Where(p=>p.FieldA==1 AND/OR p.FieldB==2); In sql it's:...
Tech-Today