Solved

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

Posted on 2008-10-16
9
1,192 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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