Disposing Question

D

Dakkar

I have a program with windows forms and after execution of my program
im making it invisible for working background progress and i have a
dispose function like this

protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
}
Process[] procuo = Process.GetProcesses();
for (int i = 0; i < procuo.Length; i++)
{

if(procuo.ProcessName.Equals("client"))
{
procuo.Kill();
}
}

base.Dispose(true);

}


if i close the program normally(pressing X before it become invisible)
its normally closing the client.exe but if i kill it from task manager
its not closing the client.exe how can i do that?
Thanks
 
S

Sean Hederman

Task manager has two modes of shutting an application down: End Task, which
sends a WM_QUIT message to the foreground window, and End Process which
calls TerminateProcess for the process.

TerminateProcess is a nasty way to shut down an app, basically all the
threads are stopped in their tracks, the DLLs unloaded and the process
closed. Your application does not have the opportunity to handle this
scenario.

From MSDN:
Terminating a process does not cause child processes to be terminated.

Neither the process nor any DLLs attached to the process are notified that
the process is terminating. A process cannot prevent itself from being
terminated.

So basically, since you appear to want to close child processes in your
application when the process is terminated, you can't. You could have your
application act as a remoting host, and if the child processes can't connect
every so often they close themselves down.

Dakkar said:
I have a program with windows forms and after execution of my program
im making it invisible for working background progress and i have a
dispose function like this

protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
}
Process[] procuo = Process.GetProcesses();
for (int i = 0; i < procuo.Length; i++)
{

if(procuo.ProcessName.Equals("client"))
{
procuo.Kill();
}
}

base.Dispose(true);

}


if i close the program normally(pressing X before it become invisible)
its normally closing the client.exe but if i kill it from task manager
its not closing the client.exe how can i do that?
Thanks
 
M

Michael Groeger

Dakkar,

you can try to override OnClose() and do the process killing there. But I am
not quite sure if anything gets fired once you killed a process. Another
way - which should work - is that you try to catch ThreadAbortException in
the Main() method running the Form. This should look something like this

public class MyForm : System.Windows.Forms.Form
{
// [...]

[STAThread]
static void Main()
{
try
{
Application.Run(new MyForm());
}
catch (ThreadAbortException e)
{
// kill process
}
}
}

Regards,
Michael

Dakkar said:
I have a program with windows forms and after execution of my program
im making it invisible for working background progress and i have a
dispose function like this

protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
}
Process[] procuo = Process.GetProcesses();
for (int i = 0; i < procuo.Length; i++)
{

if(procuo.ProcessName.Equals("client"))
{
procuo.Kill();
}
}

base.Dispose(true);

}


if i close the program normally(pressing X before it become invisible)
its normally closing the client.exe but if i kill it from task manager
its not closing the client.exe how can i do that?
Thanks
 
D

Dakkar

any idea that how can i prevent this or if i cant prevent it how can i
make my process invisible(not show in task manager)
 
P

Pipo

you can create a Windows Service or a Console Application that will not show
up in the task list.


Dakkar said:
any idea that how can i prevent this or if i cant prevent it how can i
make my process invisible(not show in task manager)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top