Question

In my program (form1) i call another form (form3) with Form3.ShowDialog(). This form runs a relativly long process that is tracked by a progress bar. In this form there is a button to cancel this process(generate pdf document) and revert the process.

The code for the cancel button (form3) is as follows:

Private Sub annulerBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles annulerBtn.Click
        If (MsgBox("Êtes-vous sûr de vouloir annuler? Cette reviendra toutes les modifications apportées au document", MsgBoxStyle.YesNo, "annuler l'exportation") = MsgBoxResult.Yes) Then

            _cancel = True

            doc.Close()       'close pdf'
            fs.Close()        'close stream'

            If (_done And _backup) Or (Not _done And _backup) Then

                'revert file from backup if backup exists'
                System.IO.File.Delete(_path)
                IO.File.Copy("C:\temp\temp.pdf", _path)
                IO.File.Delete("C:\temp\temp.pdf")

            Else

                'otherwise simply delete the new file'
                System.IO.File.Delete(_path)

            End If

            Me.Close()
        Else
            'continue with the form!!'
        End If
    End Sub

I would like this button to end the process and revert changes using the backup.

I am currently staying away from multithreading and im using an Application.DoEvents() inside the process to continue taking user input.

If the user presses the yes button, the function works as expected. However if the user presses no, the process will continue as expected, but the form will close afterwards!

Debugging shows that it it never calls Me.Close() or Form3.Close() after the user presses no.

Any help with this issue would be appreciated, Thank you!

EDIT: Here is the call stack

    App58.exe!App58.Form3.Form3_FormClosing(Object sender = {App58.Form3}, System.Windows.Forms.FormClosingEventArgs e = {System.Windows.Forms.FormClosingEventArgs}) Line 432  Basic
    System.Windows.Forms.dll!System.Windows.Forms.Form.OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) + 0x77 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Form.CheckCloseDialog(bool closingOnly = false) + 0x8c bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FContinueMessageLoop(int reason, int pvLoopData, System.Windows.Forms.NativeMethods.MSG[] msgPeeked) + 0x160 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = 4, int pvLoopData = 0) + 0x1ae bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = 4, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.Application.ModalApplicationContext}) + 0x177 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form form) + 0x33 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) + 0x370 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x7 bytes 
    App58.exe!App58.Form1.RB_pdf_Click(Object sender = {Text = "Exporter PDF"}, System.EventArgs e = {System.Windows.Forms.MouseEventArgs}) Line 1994 + 0xa bytes   Basic
Was it helpful?

Solution

A wild guess, you tell me if it is right.

the property DialogResult for the button annulerBtn is set to something different from None.
Or the property CancelButton or AcceptButton of the form3 is set to the annulerBtn

If one of this conditions is true, then your form will close itself automatically when you click that button, regardless of your calling the Close method or not. If you want to stop this chain of events you should set the form3.DialogResult property to DialogResult.None before exiting the click event. (Or remove the association between the form and the button setup by the properties above)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top