Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

So I have created an application that has a custom OnStartUp event handler.

The code within which is similar to this:

C#
m_ConfigurationManager = ConfigurationManager.InitializeInstance(Assembly.GetExecutingAssembly().GetName().Name, 
        Code.StringToSecureString());
if (m_ConfigurationManager.Instance.ReadConfiguration() == false)
{
	Wizard dlg = new Wizard("Application Name");
	if (dlg.ShowDialog() == true)
	{
		m_ConfigurationManager.Configuration = dlg.Result;
		if (m_ConfigurationManager.Instance.Configuration.VerifyData(Code))							
			m_ConfigurationManager.Instance.Configuration.SecureData();
		m_ConfigurationManager.SaveConfiguration();
	}
	else
	{
		Current.Shutdown();
	}
}

if (!m_ConfigurationManager.Instance.ReadConfiguration()) Current.Shutdown();
MainWindow mainWindow = new MainWindow();
mainWindow.Show();


Now the problem I have is this, if my configuration wizard launches and is completed succesffuly instead of the last sections of code:
C#
if (!m_ConfigurationManager.Instance.ReadConfiguration()) Current.Shutdown();
MainWindow mainWindow = new MainWindow();
mainWindow.Show();


runs through, but the application just terminates instead of showing the mainWindow. My understanding is that this is because the message pump is shut down. Is there any what I can stop this from happening?

I have tried calling "Application.Run(new MainWindow)" but this won't compile. I have also tried "this.Run(new MainWindow)" and this results in a dispatch error. (suggested from http://stackoverflow.com/[^])

Has anyone got any idea how I can keep the program running after the wizard closes and run the main window?

App.Xaml Code:

XML
<application x:class="ApplicationName.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" startup="App_OnStartup">
    <application.resources>
         
    </application.resources>
</application>
Posted
Updated 29-Apr-13 3:38am
v2
Comments
db7uk 29-Apr-13 9:32am    
what is the value of !m_ConfigurationManager.Instance.ReadConfiguration()? are you sure the current.shutdown is not being called? Is you code being set in the override OnStartup in the app.xaml.cs? Have you removed the entry for StartUpUri in the app.xaml file? Have you called the base.OnStartUp(e); in the startup event?
Pheonyx 29-Apr-13 9:38am    
Hi db7uk,

m_ConfigurationManager.Instance.ReadConfiguration() returns true if the wizard is completed, and false if the wizard is exited prematurely. I have stepped through the code to ensure this happens and the current.shutdown is not being called.

My App.Xaml has been added to the question.

And all the code above is the event handler. As you can see there is no StartUpUri set.

No I haven't tried the base.OnStartUp(e) call you are suggesting, will try that now.
Pheonyx 29-Apr-13 9:42am    
I tried the base.OnStartUp(e); call but that won't compile. Instead I get a "Cannot resolve symbol 'OnStartUp'" exception.

My app class looks like this:

public partial class App
{
private ConfigurationManager m_ConfigurationManager;

private void App_OnStartup(object sender, StartupEventArgs e)
{
//Code from above goes in here
}

}
johannesnestler 29-Apr-13 9:40am    
hmm - just a quick thought... Why don't you make it the other way round? Let the main window show (and hide) the wizard.
Pheonyx 29-Apr-13 9:42am    
The main window will not work without the initial configuration being set up. It won't even initialize as it needs data paths.

1 solution

Solution was devised with the help of Johannesnestler.
Moved the initial creation of the main form to the start of the method, but kept the show at the end after the configuration check.

Kudos to Johannesnestler for the help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900