-
Apr 12th, 2024, 03:23 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Confirmation dialog box not showing
Code:
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Try
' Check if the timer is running
If timerRunning Then
' Stop the timer and wait for it to finish
timerRunning = False
Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
End If
' Close the form
Me.Invoke(Sub() Me.Close())
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Try
' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
' If the timer is running and any of the radio buttons inside GrpOption1 is checked,
' simulate a click on the start/stop button to stop the timer
BtnExit.PerformClick()
End If
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
If I put the code below, it is not appearing.
Code:
If MessageBox.Show("Do you want to close the form?", "Confirm", MessageBoxButtons.YesNo) <> DialogResult.Yes Then
e.Cancel = True
End If
-
Apr 12th, 2024, 08:40 AM
#2
Re: Confirmation dialog box not showing
Where are you putting that code?
My usual boring signature: Nothing
 
-
Apr 13th, 2024, 12:33 AM
#3
Thread Starter
PowerPoster
Re: Confirmation dialog box not showing
 Originally Posted by Shaggy Hiker
Where are you putting that code?
in btnExit
-
Apr 13th, 2024, 12:42 AM
#4
Thread Starter
PowerPoster
Re: Confirmation dialog box not showing
I tried this one, but still no confirmation dialog appeard.
Code:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Try
' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
' Display a confirmation dialog
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
' Check the user's response
If result = DialogResult.No Then
' Cancel the form closing event
e.Cancel = True
End If
End If
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
-
Apr 13th, 2024, 01:48 AM
#5
Re: Confirmation dialog box not showing
 Originally Posted by Simply Me
I tried this one, but still no confirmation dialog appeard.
Code:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Try
' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
' Display a confirmation dialog
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
' Check the user's response
If result = DialogResult.No Then
' Cancel the form closing event
e.Cancel = True
End If
End If
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
If you put a breakpoint on the line, does the breakpoint get hit?
-
Apr 13th, 2024, 08:20 AM
#6
Thread Starter
PowerPoster
Re: Confirmation dialog box not showing
 Originally Posted by PlausiblyDamp
If you put a breakpoint on the line, does the breakpoint get hit?
it's not getting hit at all.
-
Apr 13th, 2024, 08:38 AM
#7
Re: Confirmation dialog box not showing
Code:
If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
' Display a confirmation dialog
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If that message box is not being shown then clearly at least one of those conditions is False. Put a breakpoint on the If statement and check which one. You can then work backwards to where you expected some change to occur that would make them True.
-
Apr 13th, 2024, 10:35 AM
#8
Thread Starter
PowerPoster
Re: Confirmation dialog box not showing
 Originally Posted by jmcilhinney
Code:
If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
' Display a confirmation dialog
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If that message box is not being shown then clearly at least one of those conditions is False. Put a breakpoint on the If statement and check which one. You can then work backwards to where you expected some change to occur that would make them True.
I tried putting breakpoint in if statement but when I clicked the x button or the btnexit, the form just closes with out hitting the breakpoint I put.
-
Apr 13th, 2024, 12:22 PM
#9
Re: Confirmation dialog box not showing
I wouldn't have thought so but I wonder whether your Invoke call has anything to do with it. That's pointless anyway so you should get rid of it and just call Close. See whether that makes a difference.
-
Apr 13th, 2024, 11:44 PM
#10
Thread Starter
PowerPoster
Re: [RESOLVED] Confirmation dialog box not showing
It is working now. Thanks jmcilhinney.
Code:
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Try
' Check if the timer is running
If timerRunning Then
' Stop the timer and wait for it to finish
timerRunning = False
Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
End If
' Close the form, which will trigger the FormClosing event
Me.Close()
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Try
If timerRunning Then
' Stop the timer and wait for it to finish
timerRunning = False
Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
End If
' Check if the close reason is user closing via the X button
If e.CloseReason = CloseReason.UserClosing Then
' Display a confirmation dialog
Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
' Check the user's response
If result = DialogResult.No Then
' Cancel the form closing event
e.Cancel = True
End If
End If
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
-
Apr 14th, 2024, 05:11 AM
#11
Re: [RESOLVED] Confirmation dialog box not showing
You would only need to invoke that method on the UI thread if the current method could be executing on a different thread. The Click event handler of a Button is never going to be, so invoking is pointless. If you were in the situation where you might need to call Invoke, you could check InvokeRequired first and only call Invoke if necessary. Alternatively, I wonder whether BeginInvoke may not have had the same issue.
-
Apr 14th, 2024, 06:55 AM
#12
Thread Starter
PowerPoster
Re: [RESOLVED] Confirmation dialog box not showing
 Originally Posted by jmcilhinney
You would only need to invoke that method on the UI thread if the current method could be executing on a different thread. The Click event handler of a Button is never going to be, so invoking is pointless. If you were in the situation where you might need to call Invoke, you could check InvokeRequired first and only call Invoke if necessary. Alternatively, I wonder whether BeginInvoke may not have had the same issue.
I used the invoke because I was getting invoke and begin invoke error when I click the exit button while the timer is running. Anyways, thanks much.
-
Apr 15th, 2024, 09:44 AM
#13
Re: [RESOLVED] Confirmation dialog box not showing
I have a strategy I use when closing form apps that have become 'complicated'. It is built-in to our standard app template.
We define a 'Closing / Stopping' synchronization object in the form of a Threading.ManualResetEvent. For all long running or loopy things this is checked.
When the form is closed we treat it as a two step process. Here is the basic template code for form closing.
Code:
Public Class Form1
Private isStop As New Threading.ManualResetEvent(False)
Private firstClose As Boolean = True
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If _FormClosingHelperThrd IsNot Nothing AndAlso
_FormClosingHelperThrd.Status <> TaskStatus.RanToCompletion Then
e.Cancel = True 'wait for helper to complete
ElseIf firstClose Then
_FormClosingHelperThrd = Task.Run(Sub() _FormClosingHelper()) 'fire up the helper
firstClose = False
e.Cancel = True 'cancel this closing
Else
End If
End Sub
Private _FormClosingHelperThrd As Task = Nothing
Private Sub _FormClosingHelper()
'
'this NON UI task does closing stuff
'
'when closing stuff done close again
Me.BeginInvoke(Sub()
Me.Close() 'second close
End Sub)
End Sub
End Class
Here is a longer example with some simulated 'work'. Create a form and add two labels named, Label1 and Label2. Also add a button named BtnExit.
Code:
Public Class Form1
Private isStop As New Threading.ManualResetEvent(False)
Private MyTasks As New List(Of Task)
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'generate simulated work
MyTasks.Add(Task.Run(Sub() tskTMR()))
MyTasks.Add(Task.Run(Sub() tskA()))
MyTasks.Add(Task.Run(Sub() tskB()))
Label2.Text = "RUN"
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private firstClose As Boolean = True
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If _FormClosingHelperThrd IsNot Nothing AndAlso
_FormClosingHelperThrd.Status <> TaskStatus.RanToCompletion Then
e.Cancel = True 'first close running
ElseIf firstClose Then
If MessageBox.Show("Are you sure?", "AYS", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
e.Cancel = True
Exit Sub
End If
_FormClosingHelperThrd = Task.Run(Sub() _FormClosingHelper()) 'fire up the helper
firstClose = False
Label2.Text = "FIRST CLOSE"
e.Cancel = True 'cancel this closing
Else
Stop
End If
End Sub
Private _FormClosingHelperThrd As Task = Nothing
Private Sub _FormClosingHelper()
'
'this NON UI task does closing stuff
'
Me.BeginInvoke(Sub()
Threading.Thread.Sleep(1000)
Label2.Text = "HELPER"
End Sub) 'second close
isStop.Set() 'signal that app wants to stop
'these should all be .Set when all of the tasks are done
' timeout is set in case the .Set is not reached
' IF using timeout it must be appropriate
_tskTmrComp.WaitOne()
_tskAComp.WaitOne(5000)
_tskBComp.WaitOne(5000)
Task.WaitAll(MyTasks.ToArray) 'wait for all tasks to end
_tskTmrComp.Dispose()
_tskAComp.Dispose()
_tskBComp.Dispose()
Me.BeginInvoke(Sub()
Me.Close() 'second close
End Sub)
End Sub
'<*><*><*><*> Simulated work <*><*><*><*>
Private _tskTmrComp As New Threading.ManualResetEvent(False)
Private Sub tskTMR()
While Not isStop.WaitOne(0)
_tskTmrComp.WaitOne(100) 'simulated work
Me.BeginInvoke(Sub()
Label1.Text = DateTime.Now.ToString("HH:mm:ss.f")
End Sub)
End While
_tskTmrComp.Set() 'this thread done
End Sub
Private _tskAComp As New Threading.ManualResetEvent(False)
Private Sub tskA()
While Not isStop.WaitOne(0)
Threading.Thread.Sleep(500) 'simulated work
End While
Threading.Thread.Sleep(3000) 'simulate long running ;)
_tskAComp.Set() 'this thread done
End Sub
Private _tskBComp As New Threading.ManualResetEvent(False)
Private Sub tskB()
While Not isStop.WaitOne(0)
Threading.Thread.Sleep(250) 'simulated work
End While
Threading.Thread.Sleep(1000)
_tskBComp.Set() 'this thread done
End Sub
End Class
Hope this sparks some ideas.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|