How to close Win Form without User Interaction


I have a windows Form which display reports using Crystal Report Object & then export the reports in .pdf format & save it.
This application need to run every day. I scheduled the .exe through Windows Schedular but my problem is everyday it runs & open up a new form & then the form stays there. How can I close the Form without any user as there is no one to click the 'X' button in the form.

Please suggest.

Who is Participating?
zadeveloperConnect With a Mentor Commented:
What you really should do is create a windows service (which also will start up with windows on a power failure). Then export the report using only the reportobject (ie: without placing it on a form). I have just done the same thing , only using SSRS. Works great. If you are keen for more help on this approach .. please let me know.
You should add this line to your form_load

Me.WindowState = FormWindowState.Minimized
call this.Close() in your form's code after all tasks are completed.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

kakaliAuthor Commented:
Sorry none of the above is closing the form autromatically after it is displayed. Only if I close the 'X' button then only it is closing.
Reza RadConsultant, TrainerCommented:
you must put this.Close(); or me.close() in your new form_load method. don't call it from other forms
yeah like I said: "You should add this line to your form_load" :-)

Good luck
kakaliAuthor Commented:
I am adding here

// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(986, 501);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

but this one is giving the run time error:
Cannot access a disposed object.
Object name: 'Form1'.

I even tried in the Form_Load method to call close() method as the last statement. Have you ever tried it before ? I have already tried to call the Close() method at various places but none are working. Once the Form is displayed the control does not come back to the code it is left for the user to handle the form manually.
bklmnshConnect With a Mentor Commented:
ok, first of all your application performs some operations w/out user interaction, so it would be better to use console application for this purpose.
if you still want to do this in winforms app, do the following:

add Load event handler to the form: this.Load += new System.EventHandler(this.Form1_Load);
in Form1_Load method perform all necessary operations and in the last line in this method call this.Close().
If this.Close() for some reason doesn't work, try Application.Exit or System.Threading.CurrentThread.Abort()
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
AndyAinscowFreelance programmer / ConsultantCommented:
>>I have a windows Form which display reports using Crystal Report Object & then export the reports in .pdf >>format & save it.

At the end of your code that does the above you need to add just one extra line

That should do what you want.
As a last resort ///

You could write a little executive app that gets the Forms process. Then uses TerminateProcess to kill the app.

Snippet of code here

It could also detect when the report has been written and then terminate the Form.
kakaliAuthor Commented:
Hi All
Thanks for your help. In fact Application.Exit(),form.Close() nothing worked. I wanted to keep the form application but looks like not possible so I changed it to Console Application because no other choice.
kakaliAuthor Commented:
Not the solution which I was looking for. I changed it to Console Application instead Form Application.
AndyAinscowFreelance programmer / ConsultantCommented:
From help files about Aplication.Exit()

Informs all  message pumps that they must terminate, and then closes all application windows  after the messages have been processed.

Odd that it didn't work !
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.