Jamie Taylor
Jamie Taylor

Reputation: 1802

C# WinForms ShowDialog() call arbitrary method

I'm working on a console application that creates a form to alert users of some given state - at a later stage, the code base will become a class library.

For now, however, I need to show the form (ShowDialog would be the best method here, I guess) THEN call an arbitrary method before the form closes.

As an example, I need to show the form, set the text value of a label control, wait for n number of seconds, then change the value of the label, then close the form. I know that this sounds a little trivial, but I'm trying to proof-of-concept the design.

I've taken a look around and it doesn't look like this is possible, as ShowDialog() requires me to close the form before I can continue through code listing in the calling method/class.

Here's what I have so far:

PopUpForm myForm = new PopUpForm(string messageToDisplay);
myForm.ShowDialog();
//call myForm.someMethod() here, before the form closes
//dispose of the form, now that we've no use for it
myform.Dispose();

//target method in PopUpform class
public void someMethod()
{
  lblText.Text = "Waiting for some reason";
  //wait n number of seconds
  lblText.Text = "Finished waiting. Form will now close";
  //it doesn't matter if the form closes before the user can see this.
}

It looks like ShowDialog() doesn't support this sort of behaviour. I'm looking into BackgroundWorker threads, but was wondering if anyone has any advice on this, or have encountered this before.

Upvotes: 4

Views: 1930

Answers (3)

bash.d
bash.d

Reputation: 13207

The ShowDialog-method creates a "modal" window, and usually blocks the UI until you close it (either by clicking OK or Cancel). You would need to create a WinForm yourself, can be a simple one though and create a message-pump for it. You can run your own form by calling

Application.Run(YourForm);

You would need to hold your console-thread with a mutex for example, to keep it from continuing, while the form is open. The form offers all the methods you know from WinForms like Close, where you could tell your console-thread to continue.

See this on MSDN.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564433

If you want to show the form, then continue working, then close it - you can do so via Form.Show() instead of Form.ShowDialog():

using (var myForm = new PopUpForm(messageToDisplay))
{
    myForm.Show(); // Show the form

    DoWork(); // Do your work...

    myForm.Close(); // Close it when you're done...
} 

However, if this is purely a console application (and doesn't have a message pump), then this will likely not work properly.

Other options would be to provide a timer within your Form to have it close, or pass a delegate into the Form to run your method on Show, after which it could close itself.

at a later stage, the code base will become a class library.

When you do this, you'll likely want to come up with a different mechanism to provide notifications. Coupling your library to a specific UI technology is a bad idea. It would likely be better to have your library just provide events or other notification, and allow the user to provide the UI/notification to the user.

Upvotes: 5

Pieter Geerkens
Pieter Geerkens

Reputation: 11893

If this code ie destiend to end up in a code library, I recommend against having it display any forms, ever, of its own volition. The library should generate events, or in some cases exceptions, that can be caught by the invoking application to allow it to display the form. If certain details requiring presentation to the user are internal to the library, expose a DisplayEventData() method from the library.

Upvotes: 3

Related Questions