0

I want to have a form that displays while some process is running and then closes after its finished. In my solution I am get an Exception when I run the this.Close() method and the form dose not display. I do not really want to run a second thread just for this.

Here is how I open my form:

app appFourm = new app();
appFourm.ShowDialog();

Here is the code in the form:

  namespace App
    {
        public partial class app: Form
        {
            public app()
            {

                InitializeComponent();
                // do some stuff
                this.Close();

            }...(more sudo code)
Ben
  • 3,241
  • 4
  • 35
  • 49
DarkPh03n1X
  • 600
  • 1
  • 7
  • 17
  • So basically you want a [Splash screen](http://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application)? – Sayse Jul 31 '14 at 09:59
  • 2
    Closing a form inside its own constructor is generally not a good idea. If your example ever works, the form will be closed *before* call to `ShowDialog` method. – ForNeVeR Jul 31 '14 at 10:00
  • No more like a "Working" screen with ProgressBarStyle.Marquee in it – DarkPh03n1X Jul 31 '14 at 10:02
  • Fornever - the this. close is in a function... – DarkPh03n1X Jul 31 '14 at 10:04
  • The name of the screen is irrelevant, the main code behind it is the same. – Sayse Jul 31 '14 at 10:04
  • so Threading is the answer? – DarkPh03n1X Jul 31 '14 at 10:33
  • Just in case it isn't clear yet, threading is not the answer. You do not need a second thread, and it would not even be an advantage. The problem is simply that you cannot close a form *from its constructor*. You need to call `Close` in an event handler, one for an event that gets raised *after* the form has been constructed. – Cody Gray - on strike Jul 31 '14 at 11:26
  • @CodyGray you have marked this as a duplicate of a question that is asking about closing the form in the constructor. But I don't think that this is what the OP is asking for. This is about closing a form automatically after it has completed some work. I have given an answer to this question that I think is a good fit to this question but that would be a poor fit with the question you think is a duplicate. Please can you reconsider marking this as a duplicate. – Ben Jul 31 '14 at 11:37
  • I don't know, the sample code in the question is *clearly* trying to close the form in the constructor. Just because you've answered the question doesn't make it not a duplicate. Anyway, I'm not the ultimate arbiter on this. If you don't think it is a duplicate, vote to re-open it. @ben – Cody Gray - on strike Aug 12 '14 at 08:48
  • @CodyGray thanks for your reply. Often OP sample code is not correct (that is why they are here asking for help) so I think you need to read between the lines here and not be too literal. In addition to this the OP has now marked my reply with a tick (indicating that this is really what they wanted to do). Again I would suggest that my answer is a poor fit for the one suggested as the duplicate. Can you explain to me how I can vote for it to be re-opened please? – Ben Aug 12 '14 at 10:00

2 Answers2

2

In your Program.cs create an instance of your form, call Show(). Once your main form has loaded, Close() it.

You cannot dispose of something from inside its own constructor, that's why you get the error.

LEDWORKS
  • 123
  • 5
  • The this.close is in its own method,but i think the rules still apply since the method is called in the constructor – DarkPh03n1X Jul 31 '14 at 10:35
1

You can use the Activated event of your form. I used a form form2 with a label on it label1 to display some progress. It works like this:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        Activated += Form2_Activated;
        label1.Text = "Initialised";
    }

    private void Form2_Activated(object sender, EventArgs e)
    {
        // Call methods to do work from here, for example:

        label1.Text = "Working ...";
        Refresh();

        System.Threading.Thread.Sleep(500);
        label1.Text = "Still Working ...";
        Refresh();

        System.Threading.Thread.Sleep(500);
        label1.Text = "Done!";
        Refresh();

        System.Threading.Thread.Sleep(500);
        this.Close();
    }
}

Now you can call it and it will do the work and can display progress and then close once it is finished.

Form2 f = new Form2();
f.ShowDialog();
Ben
  • 3,241
  • 4
  • 35
  • 49
  • If you want to delay the thread in your final code then you might want to consider using `Task.Delay()` instead of `Thread.Sleep()`. Check [this article](http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx) out for a more in depth explanation. – Ben Jul 31 '14 at 11:30