从任务管理器中删除进程不会触发控制台CTRL_CLOSE_EVENT [英] killing a process from task manager does not fire console CTRL_CLOSE_EVENT
问题描述
我需要在我的控制台应用程序之前做一些重要的清理
存在。我使用了控制台ctrl事件。这个效果很好,并且它会为所有情况触发
,包括CTRL_CLOSE_EVENT(如果我从X按钮关闭
应用程序)。但是在
上与MSDN指示相反
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/ handlerroutine.htm),
从任务管理器结束我的进程没有激活
CTRL_CLOSE_EVENT。
我也尝试使用Appdomain processexit任何情况都不适用于
。
这是我试过的代码:
public enum ConsoleEvent
{
CTRL_C = 0,
CTRL_BREAK,
CTRL_CLOSE,
CTRL_LOGOFF = 5,
CTRL_SHUTDOWN
}
public delegate void ControlEventHandler(ConsoleEvent
consoleEvent);
公共事件ControlEventHandler ControlEvent;
ControlEventHandler eventHandler;
public ConsoleCtrlEventsSetup()
{
//保存到私有var所以GC没有
收集它...
eventHandler = new
ControlEventHandler(Handler);
SetConsoleCtrlHandler(eventHandler,true);
}
~ConsoleCtrlEventsSetup()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true) ;
}
无效处置(布尔处理)
{
if(处置)
{
GC.SuppressFinalize(this);
}
if(eventHandler!= null)
{
SetConsoleCtrlHandler(eventHandler,
false);
eventHandler = null;
}
}
private void Handler(ConsoleEvent consoleEvent)
{
if(ControlEvent!= null)
ControlEvent(consoleEvent);
}
[DllImport(" kernel32.dll")]
static extern bool
SetConsoleCtrlHandler(ControlEventHandler e,bool
add);
}
class CrashHandler
{
public static void
MyProcessExitHandler(Consol eCtrlEventsSetup.Consol eEvent consoleEvent)
{
StopAllMotions();
}
public static void StopAllMotions()
{
StreamWriter y =
File.AppendText(@" c:\ temp\test.txt");
y.WriteLine(停止所有动作);
y.Flush();
y.Close();
}
}
public static void Main()
{
ConsoleCtrlEventsSetup cc = new
ConsoleCtrlEventsSetup();
cc.ControlEvent + = new
ConsoleCtrlEventsSetup.ControlEventHandler(MyProce ssExitHandler);
控制台.WriteLine(按任意键退出);
}
有什么想法吗?
谢谢。
>
推荐答案
shiry,
任务管理器提供了两种结束流程的方法 - 来自* Applications *
选项卡单击*结束任务*按钮,然后单击*进程*选项卡单击g结尾*按钮上的
。
*结束任务*按钮是MSDN引用的按钮。在这种情况下,ctrl事件应该是
fire。
我怀疑如果点击*结束流程*你会得到这个事件但是,
按钮。当你这样做时它甚至会警告你。
使用此按钮结束一个进程是异常终止。
-
Stoitcho Goutsev(100)[C#MVP]
" shiry" < SH **** @ gmail.com>在消息中写道
news:11 ********************* @ f14g2000cwb.googlegro ups.com ...
shiry,
Task Manager provides two ways of ending a process - from the *Applications*
tab clicking on the *End Task* button and from the *Processes* tab clicking
on the *End Process* button.
The *End Task* button is the one that MSDN referes to. The ctrl event should
fire in this case.
I doubt it that you''ll get the event if one clicks on the *End Process*
button, though. It even warns you when you do that.
Ending a process with this button is abnormal termination.
--
Stoitcho Goutsev (100) [C# MVP]
"shiry" <sh****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
我需要在我的控制台应用程序存在之前做一些重要的清理工作。我使用了控制台ctrl事件。这很好用,它会触发所有情况,包括CTRL_CLOSE_EVENT(如果我从X按钮关闭
应用程序)。但是与MSDN所指示的相反
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/handlerroutine.htm),>从任务管理器结束我的进程没有触发
CTRL_CLOSE_EVENT。
我也尝试使用Appdomain processexit,这对于任何情况都没有用。
这是我试过的代码:
public enum ConsoleEvent
{CTR> CTRL_C = 0,
CTRL_BREAK,
CTRL_CLOSE,
CTRL_LOGOFF = 5,
CTRL_SHUTDOWN
}
public delegate void ControlEventHandler(ConsoleEvent
consoleEvent);
公共事件ControlEventHandler ControlEvent;
ControlEventHandler eventHandler;
公共ConsoleCtrlEventsSetup( )
{
//保存到私有var所以GC没有收集它...
eventHandler = new
ControlEventHandler(Handler);
SetConsoleCtrlHandler(eventHandler,true);
}
~ConsoleCtrlEventsSetup()
{
Dispose(false);
}
public void Dispose()
{
Dispose (true);
}
void Dispose(bool disposing)
{
if(disposing)
{
GC.SuppressFinalize(this);
}
if(eventHandler!= null)
{
SetConsoleCtrlHandler(eventHandler,
false);
eventHandler = null;
}
}
private void Handler(ConsoleEvent consoleEvent)
{
if(ControlEvent!= null)
ControlEvent(consoleEvent);
}
[DllImport(" kernel32.dll")]
static extern bool
SetConsoleCtrlHandler(ControlEventHandler e,bool
add);
}
类CrashHandler
{
public static void
MyProcessExitHandler(ConsoleCtrlEventsSetup.Consol eEvent consoleEvent)
{
StopAllMotions();
}
public static void StopAllMotions()
{/> StreamWriter y =
File.AppendText(@" c:\ temp \ test.txt");
y.WriteLine(停止所有动作) ;
y.Flush();
y.Close();
}
}
public static void Main()
{
ConsoleCtrlEventsSetup cc = new
ConsoleCtrlEventsSetup();
cc.ControlEvent + = new
ConsoleCtrlEventsSetup.ControlEventHandler(MyProce ssExitHandler);
Console.WriteLine(按任意键退出);
}
任何想法?
谢谢。
Hi,
I need to do some important cleanup before my console application
exists. I used the console ctrl event. This is working well and it
fires for all cases, including the CTRL_CLOSE_EVENT (if I close the
application from the X botton). But on
the contrary to what the MSDN indicates
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/handlerroutine.htm),
ending my process from the task manager did not fire the
CTRL_CLOSE_EVENT.
I also tried to use the Appdomain processexit, which didn''t work for
any of the cases.
This is the code as I tried it:
public enum ConsoleEvent
{
CTRL_C = 0,
CTRL_BREAK,
CTRL_CLOSE,
CTRL_LOGOFF = 5,
CTRL_SHUTDOWN
}
public delegate void ControlEventHandler(ConsoleEvent
consoleEvent);
public event ControlEventHandler ControlEvent;
ControlEventHandler eventHandler;
public ConsoleCtrlEventsSetup()
{
// save this to a private var so the GC doesn''t
collect it...
eventHandler = new
ControlEventHandler(Handler);
SetConsoleCtrlHandler(eventHandler, true);
}
~ConsoleCtrlEventsSetup()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
}
if (eventHandler != null)
{
SetConsoleCtrlHandler(eventHandler,
false);
eventHandler = null;
}
}
private void Handler(ConsoleEvent consoleEvent)
{
if (ControlEvent != null)
ControlEvent(consoleEvent);
}
[DllImport("kernel32.dll")]
static extern bool
SetConsoleCtrlHandler(ControlEventHandler e, bool
add);
}
class CrashHandler
{
public static void
MyProcessExitHandler(ConsoleCtrlEventsSetup.Consol eEvent consoleEvent)
{
StopAllMotions();
}
public static void StopAllMotions ()
{
StreamWriter y =
File.AppendText(@"c:\temp\test.txt");
y.WriteLine("stopping all motions");
y.Flush();
y.Close();
}
}
public static void Main()
{
ConsoleCtrlEventsSetup cc = new
ConsoleCtrlEventsSetup();
cc.ControlEvent += new
ConsoleCtrlEventsSetup.ControlEventHandler(MyProce ssExitHandler);
Console.WriteLine("Press any key to exit");
}
Any ideas?
Thanks.
谢谢,
所以,这意味着我无法以这种方式捕获应用程序崩溃?
AppDomain.CurrentDomain.ProcessExit函数怎么样,它可以帮助我
如果发生崩溃?
我在某个地方读到了捕获崩溃的唯一方法,就是运行一个使用
System.Diagnostics的观察者应用程序。处理类,并获取
应用程序的返回码。那是对的吗?还有其他办法吗?
Thanks,
So, that means that I cannot catch an Application crash in this manner?
What about AppDomain.CurrentDomain.ProcessExit function, can it help me
in case of a crash?
I read somewhere that the only way to catch a crash, in order to do
some cleanup, is to run a watcher application that uses the
System.Diagnostics.Process class, and get the return code of the
application. Is that correct? Is there any other way of doing this?
" shiry" < SH **** @ gmail.com>在消息中写道
news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
"shiry" <sh****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
谢谢,
那么,这意味着我无法以这种方式捕获应用程序崩溃?
AppDomain.CurrentDomain.ProcessExit函数怎么样,它可以帮助我
如果是崩溃?
我在某处读到,为了进行一些清理,捕获崩溃的唯一方法是运行一个使用
System.Diagnostics.Process类的watcher应用程序,获取
应用程序的返回码。那是对的吗?有没有其他方法可以做到这一点?
Thanks,
So, that means that I cannot catch an Application crash in this manner?
What about AppDomain.CurrentDomain.ProcessExit function, can it help me
in case of a crash?
I read somewhere that the only way to catch a crash, in order to do
some cleanup, is to run a watcher application that uses the
System.Diagnostics.Process class, and get the return code of the
application. Is that correct? Is there any other way of doing this?
杀死一个进程不是应用程序崩溃不是吗?
杀死一个进程(来自进程资源管理器或其他),操作系统删除过程冷,没有任何信号,因为你不应该杀死一个
进程,除非它不再功能,也就是说,它已经不再是b
可运行/响应,因此操作系统没有理由发出信号
进程的信号。将被杀死。
如果您需要处理应用程序崩溃,您将需要安装一个捕获
处理程序。
威利。
Killing a process is not an application crash isn''t it?
When killing a process (from process explorer or whatever), the OS removes
the process cold, nothing is signaled because you are not supposed to kill a
process, unless it is no longer "functional", that is, it''s no longer
runnable/responding, and because of this the OS sees no reason to signal the
process that it''s going to be killed.
If you need to handle application crashes, you''ll have to install a catch
handler.
Willy.
这篇关于从任务管理器中删除进程不会触发控制台CTRL_CLOSE_EVENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!