Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody

I want design a windows form app that after complete task, Automatically will be close.

I don't like user from bellow code in form constructor, because I need form be load and after execute tasks as well as show result after that will be close:
Application.Exit();


For more information the task of this app is connecting to specify web service and sending data to this.

Tanks a lot
Posted

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:
C#
static class Program
{
     static void Main()
     {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          MainForm mainForm = new MainForm();
          Application.Run(mainForm);
          mainForm.Close();
     }
}

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
C#
Application.Run(mainForm);
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:
C#
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
    }

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    myLabel.Text = "Completed sucessfully";
    Timer tim = new Timer();
    tim.Interval = 10000;       // 10 seconds in milliseconds
    tim.Tick += new EventHandler(tim_Tick);
    tim.Start();
    }

void tim_Tick(object sender, EventArgs e)
    {
    Close();
    }

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    // Call your methods here.
    }

If your task is quick, then you could just put the code from the worker_RunWorkerCompleted method immediately after your task method calls.
 
Share this answer
 
v3
Comments
Reza Alipour Fard 20-Jan-13 4:31am    
Thank you for your attention, But my problem is:

<pre>
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm mainForm = new MainForm();
Application.Run(mainForm);
mainForm.Close();
}
}
</pre>

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.
OriginalGriff 20-Jan-13 4:46am    
Answer updated.
Reza Alipour Fard 20-Jan-13 5:23am    
Thanks A lot, Where is I can call Close method?
I don't want to call this method in constructor of form.
OriginalGriff 20-Jan-13 5:34am    
I can't tell you "Put it there" and give you a big arrow! :laugh:
I know nothing about your application and how it works.
When do you know that your task has finished? Does it run in a method? Where is that method called from?
Reza Alipour Fard 20-Jan-13 6:52am    
Thank your consideration,

I know my task when finished.
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.
Hi there,
Simply use
C#
{
    //TODO
    this.close();
}

GoodLuck,
z3ngew
 
Share this answer
 
Comments
Reza Alipour Fard 20-Jan-13 6:54am    
Please attention to question comments!!!
I know it, My problem is complex than your solution.
C#
private void Form1_Shown(object sender, EventArgs e)
{
   //your function
   //...

   this.Text = "close"; //replace text Form1 as the trigger closing application

}

private void Form1_TextChanged(object sender, EventArgs e)
{
    this.Close();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900