我制作了一个Windows应用程序,我运行它。 然后我从APPLICATION TAB中从TaskManger关闭该应用程序,然后单击END TASK BUtton。然后我的.txt登录已成功创建。 然后他们是Task MAnager中另一个名为PROCESS TAB的TAB,但当我结束该应用程序的进程时,我的日志文件就不会被创建。
这是我的代码。
public bool eventHandled;
private int elapsedTime;
public Form1()
{
elapsedTime = 0;
InitializeComponent();
//From here Log created when we click from application atb
this.Size = new System.Drawing.Size(300, 300);
this.Text = "Form1";
LogWrite("Application Start At :");
}
public static void LogWrite(string logMessage){
try {
string path = @"D:\oldCode\log.txt";
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.Close();
}
using (StreamWriter w = File.AppendText(path))
{
Log(logMessage, w);
}
}
catch (Exception ex)
{
}
}
//event handler for termination of a process
public static void Log(string logMessage, TextWriter txtWriter)
{
try
{
txtWriter.Write("\r\n"+logMessage);
txtWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
//txtWriter.WriteLine(" :");
//txtWriter.WriteLine(" :{0}", logMessage);
txtWriter.WriteLine("-------------------------------");
}
catch (Exception ex)
{
}
}
class MyApplicationContext : ApplicationContext {
private int formCount;
private Form1 form1;
private Rectangle form1Position;
////private FileStream userData;
private MyApplicationContext() {
formCount = 0;
// Handle the ApplicationExit event to know when the application is exiting.
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
// Create both application forms and handle the Closed event
// to know when both forms are closed.
form1 = new Form1();
form1.Closed += new EventHandler(OnFormClosed);
form1.Closing += new CancelEventHandler(OnFormClosing);
// form1.Closing += new CancelEventHandler(onprocesscloase);
formCount++;
// Get the form positions based upon the user specific data.
form1.Show();
// this is for PROCESS to kill it
Process myProcess = new Process();
myProcess.StartInfo.FileName = "";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcesses_Exited);
}
//METHOD TO KILL PROCESS
private void myProcesses_Exited(object sender, EventArgs e)
{
//Process ps = (Process) sender;
//messageBox.Text = ps.ProcessName;
//messageBox.Text = " has exited";
// MessageBox.Show("Exited event caught");
LogWrite("Application Start At :");
// myProcess.CloseMainWindow();
}
private void OnApplicationExit(object sender, EventArgs e) {
// When the application is exiting, write the application data to the
// user file and close it.
}
private void OnFormClosing(object sender, CancelEventArgs e) {
// When a form is closing, remember the form position so it
// can be saved in the user data file.
if (sender is Form1)
form1Position = ((Form)sender).Bounds;
}
private void OnFormClosed(object sender, EventArgs e) {
// When a form is closed, decrement the count of open forms.
// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0) {
LogWrite("Application End At :");
ExitThread();
}
}
[STAThread]
static void Main(string[] args) {
// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.
MyApplicationContext context = new MyApplicationContext();
// Run the application with the specific context. It will exit when
// all forms are closed.
Application.Run(context);
}
}
答案 0 :(得分:3)
从应用程序选项卡关闭应用程序会要求关闭应用程序,因此执行拆解代码,从流程选项卡关闭应用程序只需将其杀死,不会有任何问题,也不会执行应用程序代码。你无能为力。
答案 1 :(得分:3)
列夫是对的。从“应用程序”选项卡中单击“结束任务”时,Windows会向您的应用程序发送WM_CLOSE消息。这与您单击红色X关闭按钮或在应用程序的主窗体上调用Me.Close相同。发生这种情况时,您的表单关闭事件将被触发,您的应用程序可以正常关闭。
当您单击“进程”选项卡中的“结束进程”按钮时,Windows只会终止您的进程,并且不会触发任何事件。
您可以通过运行Spy ++来验证这一点,并让它监控发送到您的应用程序的消息。当您从“流程”标签中终止应用时,您将看不到任何消息被发送到您的应用。
就像您可以点击开始>关闭Windows一样关机和Windows将正常关闭,或者您只需从墙上拔下插头即可立即关闭计算机。
如果从“流程”选项卡终止应用程序,则无法执行任何操作。