Solved

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

Posted on 2008-10-16
9
1,194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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