0

I am working on C# window forms, in my application i am trying to execute procedures using c# window forms in page load event. I am trying for, while executing the procedure the form should display, after that form should close automatically. My code is like this

 public partial class Form1 : Form { string servername = ""; string database = ""; string password = ""; string Filepath = ""; string uid = ""; string reslt = ""; public Form1() { InitializeComponent(); GetExelData(); } private void Form1_Load(object sender, EventArgs e) { this.Close(); } } 

But with my code window form is not displaying, it is closed when the methods are executed. can you help me.

3
  • 3
    in your case you close the form right after loading it, you don't see the form at all. the question is - when you want to automatically close it? Commented Aug 5, 2014 at 9:58
  • Run Backgroundworker in page_load event. and at the end of backgroundworker,close the form. Don't put this.Close() in page_Load event Commented Aug 5, 2014 at 10:01
  • @evilone, after executing the procedures, i mean i have a method GetExelData(), after this the form close automatically. thanks for reply. Commented Aug 5, 2014 at 10:01

4 Answers 4

1

Try this:-

 private BackgroundWorker bw; private void Form1_Load(object sender, EventArgs e) { bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += BwOnDoWork; bw.WorkerSupportsCancellation = true; bw.RunWorkerAsync(); } private void BwOnDoWork(object sender, DoWorkEventArgs e) { GetExelData(); bw.CancelAsync(); if (this.InvokeRequired) { this.Invoke(new EventHandler(delegate { this.Close(); })); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Tank you Yash, it is working fine, this is what i want.
1

Move the GetExcelData() to the Form1_Load handler. (The Shown handler might be better)

This way the method will be called when the form is showing.

To close the form after the method has finished:

  • use an event or callback (if available)
  • or call the GetExcelData on a separate thread and close the form when the thread finishes

Comments

0

You could try moving the GetExcelData call to the load event:

public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { GetExelData(); this.Close(); } 

Form load only occurs when the form is fully constructed. Note however that, even with my improvement, it doesn't make sense what you're trying to do exactly.

This "delay" in closing is only because the method is executed on the GUI thread (bad practice). Are you trying to display some kind of loading indicator? There are better ways for that.

Comments

0

Use Shown event handler.
This event will be executed after form was displayed first time(just what you need).

public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { GetExelData(); } private void Form1_Shown(object sender, EventArgs e) { this.Close(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.