If you want to close a form app, then just call the Close method at the appropriate point in your code.
I suspect that what you want to do is start by handling the Form.Shown event (which is fired when the form has been displayed for the first time) send your data to the web service and then call Close.
Or am I missing something here?
"Thank you for your attention, But my problem is:
static class Program
static void Main()
MainForm mainForm = new MainForm();
I need exit from mainform after finish task.
this app execute every hours and system admin must see this result, But some times may be admin doesn't exist front of server. therefore i need close app automatically."
It doesn't work like that - you can't close the form from the Main method as the line
Does not return until the form is closed!
You have to close the form from within the form itself - either when the task is finished, or by starting a timer when the task is complete, and calling Close when the time out has expired (to give the operator say 10 seconds to see the results if he is there)
I can't be more precise, because I have no idea how you execute your task within the form.
"I call 3 methods in form constructor for do tasks, therefore i know when tasks was finished.
But i can't use Close in constructor because in this situation after finish task immediately form will be closed. and if i use from thread.sleep before close in constructor therefore form doesn't display while finished sleep time.
I want display form immediately after finished that form close after 10 second.
Right! OK, this depends on how long your task will take - if it is a few seconds or less, then keep it as it is. If not, then you should look at moving the processing code into a BackgroundWorker thread to allow the UI to complete. It's pretty easy to do:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
myLabel.Text = "Completed sucessfully";
Timer tim = new Timer();
tim.Interval = 10000;
tim.Tick += new EventHandler(tim_Tick);
void tim_Tick(object sender, EventArgs e)
void worker_DoWork(object sender, DoWorkEventArgs e)
If your task is quick, then you could just put the code from the
method immediately after your task method calls.