?
Solved

disappearing messagebox

Posted on 2005-04-06
8
Medium Priority
?
507 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:IT Meetjesland
  • 3
  • 3
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13718212
I would suggest writing the error to an error log, instead of trying to display a message box.

Bob
0
 
LVL 1

Author Comment

by:IT Meetjesland
ID: 13734228
yes, i do that too.  i just removed that code for simplicity.
0
 
LVL 1

Author Comment

by:IT Meetjesland
ID: 13734395
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
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 13735351
I believe that Application.Exit is rather abrupt and overrides a lot of things.

How would you handle this in Delphi?

Bob
0
 
LVL 1

Author Comment

by:IT Meetjesland
ID: 13762098
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13762188
You might be able to keep the MessageBox up by using a SystemModal:

Proof of concept in C#:
http://www.codeproject.com/cs/miscctrl/MessageBoxIndirectCS.asp

In Model-View-Controller, the View is the main entity.  The Controller is responsible for two-way communication between the View and the Model.  There should be a strong-bond between the View and the Controller, and in some cases, the View and the Controller are one and the same.

Bob

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question