Link to home
Start Free TrialLog in
Avatar of Pro Suite
Pro SuiteFlag for Belgium

asked on

disappearing messagebox

i have this application with a central Controller object that is launched first (before any form, as it should be by default).  If anything fails during initialisation an exception is raised, caught and a messagebox is shown.  This latter disappears most of the times without having pressed anything.  It certainly is shown because i see it pop up but it disappears again very quickly.

Since this is happening before any form is shown, i figured it may have to do something with the fact that i didn't have a messageloop at that moment yet and that the application simply shut down because of the lack of a messageloop, but it appears to be nothing to do with that because, when i changed my code so that i have a loop from the very start, the messagebox still disappeared very fast while my messageloop (and thus application) remains active.

Here's the code.  I advise to scroll down to the Main() and then look at the OnApplicationIdle() handler.  I use the Application.Idle event to launch everything, i disable the eventhandler after its first call because it only has to run once.  I had to do this because i didn't want any form to be the central object, so i had to use Application.Run() instead of Application.Run(myForm).

I hope someone can give me some clues why the message decides to not wanting to be around anymore, because i really don't get this.

Private FctrlApplication As ClsCtrlApplication
Private FbInitialized As Boolean = False

    Sub OnApplicationIdle(ByVal sender As Object, ByVal e As EventArgs)
        RemoveHandler Application.Idle, AddressOf OnApplicationIdle ' make sure all this is done only once
        FctrlApplication = New ClsCtrlApplication(GetType(ClsCtrlApplication).Assembly)
        Try
            FctrlApplication.Initialize() ' prepares logger, exceptionhandler, ...
            FbInitialized = True
            FctrlApplication.Execute() ' shows main form
        Catch Ex As Exception
            MessageBox.Show("A fatal error occurred: " & ClsChars.EOLDbl & Ex.Message, "Fatal error", MessageBoxButtons.OK, MessageBoxIcon.Error) ' this should wait for interaction, but it pops away quite fast
            Application.Exit() ' when you drop this line, the application keeps on running after messagebox disappears
        End Try
    End Sub

    Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
        If FbInitialized Then FctrlApplication.Deinitialize()
        If ClsCtrlApplication.IsInstantiated(FctrlApplication) Then
            FctrlApplication.Dispose()
        End If
    End Sub

    'This method is the entrypoint of the application.
    Sub Main()
        'Idle event is exploited to start the application controller
        AddHandler Application.Idle, AddressOf OnApplicationIdle
        AddHandler Application.ApplicationExit, AddressOf OnApplicationExit
        Try
            'start main messageloop
            Application.Run()
        Finally
            RemoveHandler Application.ApplicationExit, AddressOf OnApplicationExit
        End Try
    End Sub
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I would suggest writing the error to an error log, instead of trying to display a message box.

Bob
Avatar of Pro Suite

ASKER

yes, i do that too.  i just removed that code for simplicity.
I'm very curious now why that messagebox doesn't wait, i never wished more to be able to debug inside the framework as you can in Delphi.

Maybe i should make my own messagebox to find out what's going on.

In the meantime, thanks for replying Bob.

Thomas
I believe that Application.Exit is rather abrupt and overrides a lot of things.

How would you handle this in Delphi?

Bob
Calling Application.Exit may not be advisable at a higher level (in forms etc) but not here since nothing is left except for the win-messageloop and that is exactly what Application.Exit() terminates imho.

But the exit() can't have anything to do with the abrupt disappearance of the messagebox.  For all i care, i even may add code to nuke the whole computer.  This code may not be executed until the user clicks the messagebox away.  So, the real and only problem is that the messagebox should idle the mainthread waiting for user input which it doesn't in this case, and that isn't right.

And for your question:
In Delphi, you simply can debug inside the "framework" until you reach API level and this way at least have a much bigger chance to find the reason why the messagebox doesn't wait ( i don't know how it works in delphi 8, never used delphi again since i changed work and switched to VS.NET).

I guess nobody can help me any further on this thread?  Maybe another approach:

I wonder, how do other people implement MVC? Don't you need a central controller that starts other controllers with forms, datamodules, ... ?  How do yóu start the controller first, and not that darn main-form?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial