Solved

How to make a C# executable kill itself when it is finished running?

Posted on 2008-10-16
9
1,188 Views
Last Modified: 2008-10-16
hi all,
i am using a c# executable file to do some jobs. it is running on a certain time everyday. my problem is how to exit the process when it finish doing the job?

i try to kill it in main function, but it does not work. because i check the Task Manager, the process is still there. i have to right click the process in task manager to end it manually. How to end the process automatically? i have to put it in automation server.

[STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Form frm = new Form1();

            Application.Run(frm);//do some jobs

            frm.Close();//finished, then close

            Application.Exit();//exit, but it still stay in Task manager,how to end it automatically?

        }

Open in new window

0
Comment
Question by:viola123
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22737028
do you have some threads?
0
 
LVL 6

Author Comment

by:viola123
ID: 22737112
NO, it is just a simple exe
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22737133
the last 2 lines are never executed, at least when the main Form exists.
you should put  a timer object in your form and close the form from inside.
0
 
LVL 6

Expert Comment

by:carlsiy
ID: 22737214
you could try using Application.ShowDialog for the form to be opened  rather than Application.Run, and nest the jobs there also with the form exit, so when the application closes the subform it will continue to exit the application
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Author Comment

by:viola123
ID: 22737249
hi,
there is only one form contained. so i have to close this form first? how?
in the form1.cs, write this.close()?
0
 
LVL 6

Author Comment

by:viola123
ID: 22737271
hi,
in form1.cs:
public Form1()
        {
            InitializeComponent();
            Myjob(); //doing some jobs
            this.Close(); // close the form?
        }

pls give me some idea how to fix it?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22737301
better try to do it at Load event:
            Myjob(); //doing some jobs
            this.Close(); // close the form?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22737424
how are you running your application? through debugger or from the setup you have prepared
if through debugger then whats appearing in the task manager as the exe name
is it projectname.exe or projectname.vshost.exe?

if its vshost.exe then its ok becasue the debugger keeps it open as you can see in you bin folder 2 exes
one a normal projectname.exe and the other projectname.vshost.exe
0
 
LVL 6

Author Comment

by:viola123
ID: 22737491
HI jaime,
that works!!!!! i am very happy with that.

thanks a lot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now