7

I create a simple form that pops up a dialog box when you run it, but i cannot close it programmatically. Can anyone help me with that?

Label lb = new Label();
Form frm = new Form();
lb.Left = 100;
lb.Top = 44;
frm.Controls.Add(lb);
frm.ShowDialog();
System.Threading.Thread.Sleep(2000);

After ..Sleep(2000) i want it to close. I tried:

 frm.close();frm.dispose();frm.visible = false;

Thanks in advance!

Mark Roll
  • 506
  • 3
  • 6
  • 15

6 Answers6

7

You can use something like this inside you form class:

protected override async void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    await Task.Delay(2000);
    Close();
}

Inside the OnLoad method run a task that wait 2000 ms and close the form.

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
5

To close any form after a specific time you can use an extension method

public static class Extension
{
    public static async Task CloseAfterDelay(this Form form, int millisecondsDelay )
    {
        await Task.Delay( millisecondsDelay );
        form.Close();
    }
}

and call it before you show the form

Label lb = new Label();
Form frm = new Form();
lb.Left = 100;
lb.Top = 44;
frm.Controls.Add(lb);

frm.CloseAfterDelay( 2000 );

frm.ShowDialog();
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • Thnx for your answer, it helped me. I wanna ask if you can provide me a sample code that dialogbox will close only when i press a button.Is that possible? – Mark Roll Sep 08 '16 at 09:46
3

instead of using frm.ShowDialog(); use frm.Show();

ShowDialog() will wait for the dialog to close then continue the code.

Edited:

        Label lb = new Label();
        Form frm = new Form();
        lb.Left = 100;
        lb.Top = 44;
        frm.Controls.Add(lb);

        frm.Load += delegate (object o, EventArgs args)
        {
            Thread thread = new Thread(p =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                Action action = frm.Close;
                Invoke(action);
            });
            thread.Start();
        };
        frm.ShowDialog();
Bob
  • 545
  • 3
  • 8
  • I really need it with ShowDialog() cause in my program the show method crashes it – Mark Roll Sep 08 '16 at 07:05
  • You should mention that in your question that you have tried `Show()` and it crashes. – Bob Sep 08 '16 at 07:14
  • Holy mother, thank you so much. I was tearing my hair out. This tutorial I was following was using `ShowDialog`. I didn't realize there was a `Show` https://theitbros.com/powershell-gui-for-scripts/ – velkoon Jul 22 '21 at 22:36
2

Try with the following way. May be this is not the more appropriate way. But I think it will solve your issue:

        Label lb = new Label();
        Form frm = new Form();

        frm.Shown += Frm_Shown;

        lb.Left = 100;
        lb.Top = 44;
        frm.Controls.Add(lb);
        var result = frm.ShowDialog();   

    private void Frm_Shown(object sender, EventArgs e)
    {
        var frm = (Form)sender;
        System.Threading.Thread.Sleep(3000);
        frm.DialogResult = DialogResult.Cancel;
        frm.Close();
    }
Pabdev
  • 406
  • 3
  • 14
  • `System.Threading.Thread.Sleep(3000);` will run on the main thread and make application unresponsive for the duration of the sleep – user5226582 Sep 08 '16 at 11:24
2

ShowDialog() will display the form as a modal dialog. That means, the call will wait until the form was closed. That again means your Thread.Sleep() will be executed after the form was already closed.

The non-modal version would be Show(), but that can cause issues, because during the Sleep, the UI will not be updated, so it appears frozen. You would need to keep the message loop alive.

for (int i=1; i<40; i++)
{
    Application.DoEvents();
    Thread.Sleep(50);
}

You should understand the consequences of DoEvents() when you do that. IMHO the cleaner approach would be a timer on the form as mentioned by @Pikoh

Also note the code that follows

frm.close();frm.dispose();frm.visible = false;

After Dispose() you should not access the form any more, because it's already destroyed. Maybe that caused the crash.

Community
  • 1
  • 1
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

To close your dialog after some time, you must add a timer in your dialog form,something like this:

private void Form2_Load(object sender, EventArgs e)
{
    Timer tim = new Timer();
    tim.Interval = 2000;
    tim.Tick += new EventHandler(tim_Tick);
    tim.Enabled = true;
}

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

You can add your timer in your Form_Load or in your form's constructor.

Pikoh
  • 7,582
  • 28
  • 53
  • Thnx for your answer, it helped me. I wanna ask if you can provide me a sample code that dialogbox will close only when i press a button.Is that possible? – Mark Roll Sep 08 '16 at 09:47