Interruption of application loading process.

Topics: CAB & Smart Client Software Factory
Sep 22, 2007 at 7:44 AM
How I can to teminate application while it's loaging? For ex., in some module controler.
if (somecondition)
{
/// terminate application
}

Thanks.
Developer
Sep 24, 2007 at 4:47 PM
Hi

You can use EventBroker to make this task.
For example, you can publish an event in your class ModuleController the following way:

// ModuleController class.
 
[EventPublication(EventTopicNames.CloseApp, PublicationScope.Global)]
public event EventHandler<EventArgs> CloseApp;
 
public override void Run()
{
    if (SomeCondition())
        OnCloseApp();
    else
    {
        AddServices();
        ExtendMenu();
        ExtendToolStrip();
        AddViews();
    }
}
 
protected virtual void OnCloseApp()
{
    if (CloseApp != null)
    {
        CloseApp(this, EventArgs.Empty);
    }
}
and Subscribe from your Application's Shell:

// ShellForm class.
 
[EventSubscription(EventTopicNames.CloseApp, ThreadOption.UserInterface)]
public void OnCloseApp(object sender, EventArgs eventArgs)
{
    this.Close();
}
Please let me know if this helps!

Mariano Converti
http://staff.southworks.net/blogs/mconverti/
Sep 25, 2007 at 9:14 PM
Hi, Mariano. Thanks for answer!!!
I hit upon to the same of solution one to one, but there some issue:

After "this.Close();" ShellForm is destroyed, but then the flow of application continues and go...
public override void Run()
{
if (SomeCondition())
OnCloseApp();
else
{
AddServices();
ExtendMenu();
ExtendToolStrip();
AddViews();
}
// !!!!go here!!! without break!!!!!
}

There is throw exception on next module with view, because parent form is already destroyed.

I hope that you correctly understood me. To me it is difficult to express thought in the English.

Developer
Sep 26, 2007 at 4:08 PM
Hi

I understood what are you asking.

You probably need to move the code, for example, to the Presenter class of your view in of this way:

// Presenter class.
[EventPublication(EventTopicNames.CloseApp, PublicationScope.Global)]
public event EventHandler<EventArgs> CloseApp;
 
public void CloseApplication()
{
    OnCloseApp(EventArgs.Empty);
}
 
protected virtual void OnCloseApp(EventArgs eventArgs)
{
    if (CloseApp != null)
    {
                CloseApp(this, eventArgs);
    }
}
Later, from your view, you must call the CloseApplication method of the presenter when your condition is accomplish, for example, when the user clicks a button.

This way you avoid an exception to be thrown.

Please let me know if this helps!

Mariano Converti
http://staff.southworks.net/blogs/mconverti/
Oct 4, 2007 at 7:11 PM
Hi, Mariano.
Thanks for that you help!!!!!!!!!!!!!!!!!

Try to paste some code for ex. in AfterShellCreated() method:

protected override void AfterShellCreated()
{
base.AfterShellCreated();

// my Slice of a code
this.Shell.Close();
//
}

Whether the application will be closed? No, in my case.

I've exception access to the liquidated object is impossible then. Because other modules continue to create Views.
You too?

Any ideas?

Thanks!

Developer
Oct 8, 2007 at 4:13 PM
This error occurs because after the AfterShellCreated method, CAB starts to load all the modules. Then when a module wants to access to the shell form, an exception is thrown because it's accessing to a disposed object.
If you want to terminate the application in the ModuleController class or in the AfterShellCreated method, you must invoke the following code when your condition is satisfied:

System.Environment.Exit(0);
Instead of call:

this.Shell.Close();
Please let me know if this helps.

Mariano Converti
http://staff.southworks.net/blogs/mconverti/
Oct 11, 2007 at 5:11 AM
Hi, Mariano.
Great Thanks!!!!

Your advice has very much helped !!!!
THANKS!!!