Closing (Exiting) and Application before Application.Run() is called? - winforms

I'm having trouble understanding how to close an C# .NET winforms application. What I'm trying to do is:
Display a form to allow the user to set-up the environment how they want
If the user presses the "OK" button, perform some logic for setting up the application environment (instantiate objects, etc)
If the user presses "Cancel" or closes the window, close the application.
The problem is, I'm calling the environment set-up form before the main (1st) form. It's a recent requirement change, and I didn't fancy re-writing the code that I have from the very beginning.
The code I have (which should make more sense than my little preamble) is:
public MainForm()
{
InitializeComponent();
//Get the user to set-up the environment (load specific config files, etc)
environmentSetupForm newEnvrionmenSetupForm = new environmentSetupForm ();
if (newEnvrionmenSetupForm .ShowDialog() == DialogResult.OK)
{
newEnvrionmenSetupForm .Close();
//some logic based on what the user selected on the set-up form
}
else
{
//Since the user didn't set-up the environment correctly (there
//was a message box, informing them and giving them another
//shot at it), exit out of the application.
Application.Exit();
}
}
My only problem is that after Application.Exit(), the stack jumps back to Program.cs and performs
Application.Run(new MainForm());
So the Main form (and app) runs regardless. Is there a better way to do what I'm trying to do?
Edit: for clarity my program.cs code reads:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace myNamespace
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}

Neither the form's constructor nor its OnLoad or Load event are good places to put this code. The constructor runs due to the new MainForm() statement in the Main() method, before the Application.Run() call. The Load event is fired because the Application class calls the Show() method, hidden inside the framework code just before Application.Run() enters the message loop. Application.Exit() cannot do anything until that message loop starts running.
The workaround is to move this code to the Main() method in Program.cs. Make it look similar to this:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm main;
using (var dlg = new environmentSetupForm()) {
if (dlg.ShowDialog() != DialogResult.OK) return;
// Use dlg values
//...
main = new MainForm();
// Make main form show up at the same location
main.StartPosition = FormStartPosition.Manual;
main.Location = dlg.Location;
}
Application.Run(main);
}

Move the logic from the constructor to the main method.
Since it's related to the start up of the application and not the form it makes sense to have it as part of the start up logic.
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var newEnvrionmenSetupForm = new environmentSetupForm ();
if (newEnvrionmenSetupForm .ShowDialog() == DialogResult.OK) {
newEnvrionmenSetupForm .Close();
//some logic based on what the user selected on the set-up form
var mainform = new MainForm();
Application.Run(mainform);
}
}
Having that kind of logic, especially because it's modal and halts the execution, in a constructor is not a good idea. You want your constructors to be simple initialization of the object and to be repeatable. E.g. if you at some point needed to construct the main form again you wouldn't want the popup at that point in time I guess

Move your code below "InitializeComponent();" to MainForm_Load Event
at Mainform_Load Event instead to do an Application.Exit() just Close() the Form, this should close the app as well.

Related

How to call a method from WindowsForms in Monogame?

Hi im currently working on a project. My main form is a form and whenever i click on a button the monogame program starts. This works.
Now i made a method in the main form and i want to pass the bool to the monogame form.
Main Form method: (if checkbox is checked monogame should draw a skyline)
public bool skyCheck()
{
if (checkBox1.Checked == true)
{
sky = true;
}
else
{
sky = false;
}
return sky;
Monogame check:
if (skyCheck() == true)
{
DrawSky();
}
This gives me the name 'skyCheck' does not exist in the current context.
I made a Control that embeds a monogame into a form so that a seperate program doesn't have to be run. It isn't the normal embedded monogame you see that only gives you a graphics device and no update or game methods. it's an actual monogame embeded.
This is not the source of your problem but it could help fix it along with make your program better.
Here is the source and some brief documentation is contained in the Readme on how to use it
Pass a reference of the form to the constructor of Game1:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Change the Form1 to the name of the form class.
Form1 form;
//...
public Game1(Form1 form)
{
this.form = form;
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
//...
// I will assume the DrawSky() should be called in Draw.
protected override void Draw(GameTime gameTime)
{
//...
if (form.skyCheck()) // the "== true" is redundant.
{
DrawSky();
}
//...
}
}
The following code would normally be in Program.cs, but as a Window Forms application, this code would be in the form that launches the game:
game = new Game1(this); // where "this" refers to the current form
game.Run();
Please note the caveats to mixing Windows forms and MonoGame:
The form's message pump and the Game run in the same thread. Any stall on the form will cause the game to lag.
There will be a performance penalty.
Make sure the game shuts down properly before the form is unloaded to ensure the resources are cleaned up properly.
It is is possible to start another thread for the game to run in(thus bypassing the first two caveats), but, all communication between the two threads must be thread safe. Thread safety is guaranteed for a Boolean assignments, as well as Integer assignments (=< 32 bits for 32 bit processes, and =< 64 for 64-bit processes).
Exit coordination in both threads is required.

Launching a CustomAction UI written with WPF from a WIX installer: Fail on STAThread issue

My WIX installer launches an immediate custom action.
The custom action starts a WPF dialog prompting the user for a BD connection parameters (I basically wrote the 'new connection' DB prompter dialog in WPF, to get me a connection string that the custom action can inject in the installed application's configuration file).
The WIX code is fairly simple to figure out, and I know I execute the custom action just fine - I put there a MessageBox and a MmsiBreak on the call to my custom action method. I get there without a problem.
When the custom action instantiates my WPF dialog window, I get an InvaliOperationException: "The calling thread must be STA, because many UI components require this".
The same code runs fine when I put it in a standard WPF application, because VisualStudio generates boiler plate code with Main() that has a STAThreadAttribute on it.
I can't tack that attribute on the msiexec caller, and if I try to set the thread apartment state in my custom action, it fails:
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
Is not supposed to work for framework past 2.0.
Is there any way to do what I'm trying to do here? I'd appreciate some pointers.
EDIT
I even tried to run the dialog in its own thread, e.g. the code is like this:
// Static class members
static ManualResetEvent _done = new ManualResetEvent(false);
static ActionResult _caResult;
static Session _session;
static Thread _worker;
[CustomAction]
static public ActionResult PromptForDB(Session session)
{
_session = session;
_worker = new Thread(WorkerThread);
_worker.Start();
_done.WaitOne();
return _caResult;
}
[STAThread]
static void WorkerThread()
{
try
{
Prompter wnd = new Prompter();
if (!(bool)wnd.ShowDialog())
{
_caResult = ActionResult.SkipRemainingActions;
}
else
{
// Harvest our properties (omitted from this post)
_caResult = ActionResult.Success;
}
catch (Exception ex)
{
_caResult = ActionResult.Failure;
_session.Log("Error: " + ex.Message);
}
finally
{
_done.Set();
}
}
That does not work either.
Before starting your new thread, set its ApartmentState as follows:
_worker.SetApartmentState(ApartmentState.STA);
See this:
The calling thread must be STA, because many UI components require this in WPF

SplashScreen and custom messages

Is it possible to use the SplashScreen class in .Net to show dynamic messages while my application is loading?
Something like.
Module one loaded...
Module two loaded...
and so on.
No, you need to code this functionality yourself. The built-in splash screen can only show a static image.
You can do this using 'System.Theading'. The following code launches a "splash screen" on a separate thread whilst your application (in my example below it is called MainForm()) loads or initialises. Firstly in your "main()" method (in your program.cs file unless you have renamed it) you should show your splash screen. This will be a WinForm or WPF form that you wish to show the user at start up. This is launch from main() as follows:
[STAThread]
static void Main()
{
// Splash screen, which is terminated in MainForm.
SplashForm.ShowSplash();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Run UserCost.
Application.Run(new MainForm());
}
In your SplashScreen code you need something like the following:
public partial class SplashForm : Form
{
// Thredding.
private static Thread _splashThread;
private static SplashForm _splashForm;
public SplashForm()
{
InitializeComponent();
}
// Show the Splash Screen (Loading...)
public static void ShowSplash()
{
if (_splashThread == null)
{
// show the form in a new thread
_splashThread = new Thread(new ThreadStart(DoShowSplash));
_splashThread.IsBackground = true;
_splashThread.Start();
}
}
// Called by the thread
private static void DoShowSplash()
{
if (_splashForm == null)
_splashForm = new SplashForm();
// create a new message pump on this thread (started from ShowSplash)
Application.Run(_splashForm);
}
// Close the splash (Loading...) screen
public static void CloseSplash()
{
// Need to call on the thread that launched this splash
if (_splashForm.InvokeRequired)
_splashForm.Invoke(new MethodInvoker(CloseSplash));
else
Application.ExitThread();
}
}
This launches the splash form on a separate background thread allowing you to proceed with the rendering of your main application simultaneously. To display messages about loading you will have to pull information from the main UI thread, or work in purely aesthetic nature. To finish off and close the splash screen down when your application has been initialised you place the following inside the default constructor (you could overload the constructor if you wanted to):
public MainForm()
{
// ready to go, now initialise main and close the splashForm.
InitializeComponent();
SplashForm.CloseSplash();
}
The code above should be relatively easy to follow.
I hope this helps.

How to interrupt a constructor of window form?

I have a simple application:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Constructor of Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// ...
if (some_condition)
{
DialogResult dr = MessageBox.Show("Do you want to continue ?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (dr == DialogResult.No)
{
// How to close the window normally or how to not create a Form1 instance ?
//
//
}
}
// amount of code that executes only if some_condtion == false
}
}
I know that I can check some_condition before Application.Run but it's difficult to change (believe me). I need to check some_condition in constructor of Form1. If some_condition == true and answer is no --> application closes.
The constructor purpose is to create an instance of an object. Therefore, it should not fail unless some premise of the object fails. In this particular case, your only option is to thrown an exception.
In other words: you call the constructor of an object in order to create an instance of this object, so this is an atomic function and should not fail unless you have a technical problem.
Now, what you want is to avoid the form being displayed under some conditions. There are some ways of doing this. First, let's look at the code:
Application.Run(new Form1());
This snippet is doing two things: creating an instance of Form1 and displaying it (by feeding it to Application.Run). So one option is:
// snippet at program.cs
Form1 mainForm = new Form1();
if (mainForm.IsValid)
{
Application.Run(new Form1());
}
// snippet at Form1.cs
public bool IsValid
{
get
{
// evaluate all conditions that should determine if the form is to be showed.
return condition;
}
}
You add code at program.cs to check if the form is valid. One way is to expose this as a property in the form. Of course, if you are following separation of concerns, maybe this would be a method/property of your domain, but it's your call where to put it in.
This method/property (called "IsValid" in my code) has the responsibility to check if the form can be displayed at that time.
Remember to decouple logic from presentation in your program.
That would do it.
It will be easier to accomplish this in the form load event. You can just call Close().
Calling Close() in the constructor will cause problems because the window is not yet open. You'll end up with a disposed window that is still trying to open.

wpf detect open window

In my WPF app (csharp) I have an event handler that when triggered will open a new window (window B) of the application and display some data. However, when the event is triggered again, if the new window (window B) is still open, I don't want to spawn another instance of window B but just update the data being displayed in the current instance. So the question is: How to detect if window B is already and only open if it is not already, otherwise just update the data?
I found the Application.Current.Window collection but somehow that isn't working for me yet. Ideas?
You could create a LoadWindow() method in WindowB that you can call to load (or refresh) the data & that will work regardless of if the window is already open or not. Have it take a delegate to call when this window gets closed:
private Action ParentCallbackOnClose;
public void LoadWindow( Action parentCallbackOnClose ) {
// load the data (set the DataContext or whatever)
ParentCallbackOnClose = parentCallbackOnClose;
// Open the window and activate/bring to the foreground
Show( );
Activate( );
}
and have your window closed event call the close delegate:
private void WindowClosed( object sender, EventArgs e ) {
ParentCallbackOnClose.Invoke( );
}
Now, from your class that opens Window B, have it hold onto that instance it opens, so that if WindowB is already open when someone tries to reload it, it just calls LoadWindow on the existing instance. Something like...
private WindowB WinB;
private void LoadWindowB(Content content)
{
if (WinB == null ){
WinB = new WindowB( );
}
WinB.LoadWindow(content, WindowBClosed);
}
And then you can just have it null out WinB on that close callback so if WinB is closed, then the next time LoadWindowB() is called it will create a new instance of it:
private void WindowBClosed( ){
WinB = null;
}
Since this is the first link Google listed, which posted several years ago, for a solution to check if a Window is already open, I'll post my answer, for others, which I find easier to implement. The ChildWindow is only called from MainWindow so no other Window will need to do any checks.
private void OpenChildWindow()
{
if (this.OwnedWindows.OfType<ChildWindow>().Count() > 0)
{
ChildWindow Win = this.OwnedWindows.OfType<ChildWindow>().First();
Win.Activate();
}
else
{
ChildWindow Win = new ChildWindow();
Win.Owner = this;
Win.Show();
}
}
There is an old school way to do this using an interface. I see this in Java a lot as a way to compensate for not having delegates (correct me if I am wrong). This method will allow you to check if there is a window already open (of any kind). The original response works very well, but you can also do it the following way:
Create the interface
public interface IWindowTracker
{
void WindowIsOpened();
void WindowIsClosed();
}
Implement the interface on the parent (from where you are opening):
public partial class MainWindow : Window, IWindowTracker
In your constructor, accept an object that is of the IwindowTracker interface. Save the instance for future use
IWindowTracker windowTracker;
public ProjectManager(IWindowTracker parentWindowTracker)
{
windowTracker = parentWindowTracker;
InitializeComponent();
}
Setup the calls to the window tracker object
protected override void OnActivated(EventArgs e)
{
windowTracker.WindowIsOpened();
base.OnActivated(e);
}
protected override void OnClosed(EventArgs e)
{
windowTracker.WindowIsClosed();
base.OnClosed(e);
}
and finally implement the IWindowTracker in your parent WPF window
bool windowIsOpen = false;
public void WindowIsOpened()
{
windowIsOpen = true;
}
public void WindowIsClosed()
{
windowIsOpen = false;
}
This will allow you to keep track of if the window is still open and if it is, there is no need to open a new instance of it:
if (!windowIsOpen)
{
remoteProjectManager = new ProjectManager(this);
remoteProjectManager.Show();
}
remoteProjectManager.Focus();
Calling show() on a closed window seems to throw an exception, so my guess is that there is some other way or that if you have closed the window, the window is technically "destroyed"
The nice thing to this is that I can detect if the window is still open and focus on it (so that it comes to the front again).
NOTE: There is a draw back to this, in that in this setup it limits you to opening only one window at a time (assuming that all your windows are implemented like this). In my case, I only ever want to have one window open besides the main window.
You might also want to check if your window is null or not, considering that it probably isn't the only window you will have to open.
edit: oops, my answer is specific to Windows Forms. i just now saw the WPF mention. i'm not sure what the specific code would be for WPF, but i would imagine that it's not all that different conceptually. I think in WPF the property is called IsVisible instead of Visible
You could hold on to the instance of your window (or make it a Singleton) and then when you need to determine if it is visible or not, check it's Visible property.
for example:
if(myWindow.Visible){
myWindow.Hide();
}else{
myWindow.Show();
}
This article it the best I found for passing data between WPF pages. The author used KISS approach to provide a simple solution.

Resources