Solved

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

Posted on 2008-10-16
9
1,190 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

11 Experts available now in Live!

Get 1:1 Help Now