Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 19-Jan-13 22:01pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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()
     {
          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
          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:
            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.
  Permalink  
v3
Comments
Reza Alipour Fard at 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 at 20-Jan-13 4:46am
   
Answer updated.
Reza Alipour Fard at 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 at 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 at 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.
OriginalGriff at 20-Jan-13 7:01am
   
Answer updated
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi there,
Simply use
{
    //TODO
    this.close();
}

GoodLuck,
z3ngew
  Permalink  
Comments
Reza Alipour Fard at 20-Jan-13 6:54am
   
Please attention to question comments!!!
I know it, My problem is complex than your solution.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 _Amy 235
2 Andreas Gieriet 180
3 Peter Leow 175
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,597
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100