Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

VB.NET button still works when disabled?

I have a simple VB.NET form that calls a SQL Server Agent Job.  My problem is that I am disabling the button for 3 seconds once depressed so the job has time to finish.  If I hit the button several times in that 3 seconds, the button continues to function even though disabled....

What am I missing?

    Private Sub btnCallJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCallJob.Click

        txtPkgStatus.Clear()
        txtPkgStatus.Refresh()
        Cursor = Cursors.WaitCursor
        BtnCallJob.Enabled = False

        'Call routine that runs sql server package and sleep 3 seconds.
        Call RunPackage2()

        Cursor = Cursors.Default
        BtnCallJob.Enabled = True

    End Sub

Thanks, Bill
0
FrancOIT
Asked:
FrancOIT
  • 7
  • 5
  • 2
  • +2
1 Solution
 
CodeCruiserCommented:
Try adding a

Application.DoEvents

after

        BtnCallJob.Enabled = False

This is winforms right? If its ASP.NET then the button has to be disabled on client side using JavaScript.
0
 
BlueMurderCommented:
you can try something like...

Do While Not myProcess.HasExited
                Application.DoEvents
Loop

Open in new window

0
 
-Alan-Commented:
Your long job, RunPackage2, should be executed on a different thread. Once your task is done, you can then Invoke a method to re-enable the button. I would execute a method using the ThreadPool class to queue a task. In your method, call your RunPackage2 method, and then Invoke to re-enable the button.
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
FrancOITAuthor Commented:
I actually had the Application.DoEvents after the BtnCallJob.Enabled = False but took it out because I didn't think it was doing anything.  After putting it back in, I discovered that if I hit the button 4 or 5 times, the click event seems to only be happening 2 times now.  SO it's better but still not correct.
0
 
FrancOITAuthor Commented:
I will have to look into calling the Threadpool Class.  I've never done that before as I don't do much VB.NET programming.  I used to do some VB6 programming though, years ago.  I am a SQL DBA.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Call DoEvents() immediately BEFORE you set Enabled() to TRUE:
    Private Sub btnCallJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCallJob.Click
        txtPkgStatus.Clear()
        txtPkgStatus.Refresh()
        Cursor = Cursors.WaitCursor
        BtnCallJob.Enabled = False

        'Call routine that runs sql server package and sleep 3 seconds.
        Call RunPackage2()

        Cursor = Cursors.Default
        Application.DoEvents()
        BtnCallJob.Enabled = True
    End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*I agree with the threading comments though.  You can place a BackgroundWorker() control on your form and use that for easy threading.
0
 
FrancOITAuthor Commented:
Thanks.  This fixed the problem.
0
 
FrancOITAuthor Commented:
Sorry, I probably should have given points to that solution as well.  I haven't used EE much.  How do you determine how many points to assign a solution?
0
 
-Alan-Commented:
Here is the solution in VB to your problem. What it demonstrates is that you should run your task on a different thread then the UI thread. Then when it is finished you can call the UI thread to re-enable the button.


Imports System
Imports System.Threading

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' first disable your button
        Button1.Enabled = False

        'you need to run your long process on a different thread
        'the current thread from button1_click is the UI thread
        'it controls the GUI, and if you do anything too long on
        'the UI thread the GUI will not function properly

        ' queue the long task to the thread pool
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DoMyTask))
    End Sub
    ' this is what the thread runs it calls your task,
    ' then asks the UI thread to renable the button
    Private Sub DoMyTask(o As Object)
        RunPackage2()

        ' renable button, anything that manipulates controls
        ' must be execute on the UI thread (use Invoke)
        Invoke(New ThreadStart(AddressOf EnableButton))
    End Sub

    ' this represents your long task
    Private Sub RunPackage2()
        System.Threading.Thread.Sleep(3000)
    End Sub

    ' renables the button
    Private Sub EnableButton()
        Button1.Enabled = True
    End Sub

End Class
0
 
-Alan-Commented:
Calling DoEvents is bad practice!
0
 
FrancOITAuthor Commented:
Alan, So how do I give you points, now that I already said that the DoEvents fixed my problem?  

Thanks a LOT for your input.
0
 
-Alan-Commented:
I dunno! This was my very first post to EE!
0
 
FrancOITAuthor Commented:
Geez.  I really appreciate your help.
0
 
-Alan-Commented:
Another good point about my solution. Your GUI will lockup completely if you don't do it on another thread. With my solution you can still interact with other parts of the interface (if you don't want this disable the whole form) and move the dialog around
0
 
FrancOITAuthor Commented:
Thanks!  Again, I'm not an expert .Net programmer.  I'm just writing a front end for one of our staff to call a SQL Agent job (that runs an SSIS package).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now