Results 1 to 5 of 5

Thread: [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    5

    [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

    Hi folks.

    I have an app which has been working fine for about 2 months. Yesterday, I carried out a maintenance session which was mainly simple bug fixing and moving my code into regions to help navigating through it. Along the way, 2 things have started happening which has totally broken my app.

    MSDN states that Application.Exit does not cause the Form.Closed or Form.Closing events to fire.

    http://msdn.microsoft.com/en-us/libr...it(VS.71).aspx

    However, my Form.Closing event is firing immediately after Application.Exit which is in a button click handler on the form. The code is like this:

    Code:
    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
    
            If CurrentSite.SiteHasChanged(OldSite) Or CurrentSite.NewSite Then
                If MsgBox("Do you want to save your changes?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton1, "Please confirm") = MsgBoxResult.Yes Then
                    Me.Close()
                Else
                    Application.Exit()
                End If
            Else
                Application.Exit()
            End If
    
        End Sub
    When I step through, my code hits the Application.Exit through either path and the Form.Closing event fires immediately afterwards.

    The second problem is that RaiseEvent inside my custom class has stopped working.

    Here's how I declare an instance of the class in the main form class of my app:

    Code:
    Friend WithEvents CurrentSite As New DomainSite   ' the site currently being edited
    Within the class, I have a callback delegate being called from a background worker thread which is carrying out a blocking task in the background to keep my UI responsive. The callback delegate gets called correctly at the end of the task and my code hits this line:

    Code:
    RaiseEvent HostNameChanged()
    Back in the form, I have an event handler on the CurrentSite instance declared as:

    Code:
        Private Sub CurrentSite_HostNameChanged() Handles CurrentSite.HostNameChanged
            UpdateNetworkConnectors()
            lblHostName.Text = CurrentSite.HostName
        End Sub
    This has been working just fine for 2 months and just suddenly stopped. I have not changed the class declarations, instance declarations, scopes or constructors.

    The third symptom is that clicking on the "View Application Events" button on the application properties page (I have enabled the Application Framework) usually presents templates of the application events. It no longer does, so I added a handler for UnhandledException manually. This does not fire either!

    What on earth has gone wrong? Tearing my hair out. The only option I can see now is to create a new project and start copy/paste but the risk is too high if someone could guide me out of this pickle.

    Help????

    Cheers

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

    Are you sure that it's the Closing event that is being raised and not the FormClosing event? They are two different events. In .NET 1.x only the Closing and Closed events existed and calling Application.Exit didn't raise either. From .NET 2.0 the Closing and Closed events still exist and the FormClosing and FormClosed events have been added. Calling application still doesn't raise the Closing or Closed events but it does raise the FormClosing and FormClosed events.

    Make sure you read what is actually on the page because Form.Closing and FormClosing are absolutely NOT the same thing. Every dot is absolutely critical in programming.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    5

    Re: [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

    Hi J

    Thanks for the response. BTW, I am very familiar with all of VB, VS and OOP - thanks for the tip about dots ;-)

    As I understand it, Form.Closing was deprecated in .Net 2.0 and replaced with Form.FormClosing although it's still in the Forms namespace. Intellisense doesn't show Closing unless you enable deprecated members in options. I'm now confused by the MSDN docs on Application.Exit as it's not clear how to prevent form closing code executing after an app exit.

    This leads me to conclude that I have a design problem. I'll go back to the white board and post back the outline of my solution...(in a nice .Net OOP fashion of course)

    Cheers

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

    From MSDN:
    Exit raises the following events and performs the associated conditional actions:

    A FormClosing event is raised for every form represented by the OpenForms property.
    Whats confusing about that... It clearly states that the FormClosing event will be raised for all forms that are currently open. Am I missing something?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    5

    Re: [2008] My app has gone beserk. Application.Exit and RaiseEvent problems

    There is a nice neat solution - staring at me!

    The FormClosing handler is passed an instance of System.Windows.Forms.FormClosingEventArgs.

    This gave me:

    If e.CloseReason = CloseReason.UserClosing Then

    Job done. I can now call routines out of my FormClosing event only if the user has closed the app and not for Application.Exit, killing from Taskman etc.

    Back to the RaiseEvent issue - a little Googling suggests that this is a common problem although I still can't figure it out. Definitely didn't change any scopes or instance lifetimes.

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width