3

I have a form that prompts a user for confirmation before running a BackgroundWorker that performs some calculations. Those calculations can take anywhere from 10-30 seconds to run and I want to make sure that once the calculations begin running, they are allowed to finish uninterrupted.

Is there a way to temporarily disable the Close Button in the title bar until the BackgroundWorker finishes its job?

I found a couple similar questions but they look like a more permanent solution (here and here). I'd like the Close Button to be disabled only temporarily while the BackgroundWorker does its job.

Any help would be much appreciated. Thanks!

Community
  • 1
  • 1
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50

4 Answers4

3
Private ImBusy As Boolean = False

Private Sub LookBusyForTheBoss()
    Me.UseWaitCursor = True
    Me.Cursor = Cursors.WaitCursor
    Me.Enabled = False
    ProgressBar1.UseWaitCursor = False
    ProgressBar1.Style = ProgressBarStyle.Marquee

    ImBusy = True
End Sub

Private Sub Form77_FormClosing(...) Handles Me.FormClosing
    If ImBusy Then e.Cancel = True
End Sub

Private Sub OkHeIsGone()
    Me.UseWaitCursor = False
    Me.Cursor = Cursors.Default
    Me.Enabled = True

    ImBusy = False
End Sub
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
0

Disabling the close button would be bad taste in the extreme. Fix your application so that the calculation can be interrupted.

  • The calculation is actually performed by a web service I don't have any control over. My BackgroundWorker sends the request, waits, and does stuff with the results. If the form is cancelled, I'll have no way of knowing how the web service responded or if it completed the job. Is there another universally accepted measure taken to handle this sort of thing? – TheIronCheek Sep 10 '15 at 21:01
  • @Sam, You don't need to know what it returned if the form closed. – OneFineDay Sep 10 '15 at 21:09
0

I agree with not hiding the 'X' but if you want it really bad I think this over here is what you are looking for:

Disable Close Button in Vb.Net

David
  • 306
  • 6
  • 20
  • How do I re-enable it once the task is complete? – TheIronCheek Sep 10 '15 at 20:59
  • `Protected Overrides ReadOnly Property CreateParams() As param.ClassStyle = param.ClassStyle And (Not &H200)` This should do the work within the same type of method – David Sep 10 '15 at 21:11
0

Something along these lines might work:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    doNotClose = True
    Do
        '
        'etc
        '
        'simulate long running 
        For x As Integer = 1 To 1000
            Threading.Thread.Sleep(10)
        Next
        Exit Do
    Loop
    doNotClose = False
    If closerequested Then
        Me.BeginInvoke(Sub()
                           Me.Close()
                       End Sub)
    End If
End Sub

Dim doNotClose As Boolean = False
Dim closerequested As Boolean = False

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    closerequested = True
    If doNotClose Then
        e.Cancel = True
        Exit Sub
    End If
End Sub

or this

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    doNotClose = True
    Do
        '
        'etc
        '
        'simulate long running 
        For x As Integer = 1 To 1000
            Threading.Thread.Sleep(10)
        Next
        Exit Do
    Loop
    doNotClose = False
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If closerequested Then
        Me.Close()
    End If
End Sub

Dim doNotClose As Boolean = False
Dim closerequested As Boolean = False

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    closerequested = True
    If doNotClose Then
        e.Cancel = True
        Exit Sub
    End If
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33