Solved

VB.NET button still works when disabled?

Posted on 2012-04-05
16
361 Views
Last Modified: 2012-06-27
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
Comment
Question by:FrancOIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +2
16 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37811639
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
 

Expert Comment

by:BlueMurder
ID: 37811678
you can try something like...

Do While Not myProcess.HasExited
                Application.DoEvents
Loop

Open in new window

0
 

Expert Comment

by:-Alan-
ID: 37811839
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:FrancOIT
ID: 37811869
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
 

Author Comment

by:FrancOIT
ID: 37811910
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 30 total points
ID: 37811933
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37811941
*I agree with the threading comments though.  You can place a BackgroundWorker() control on your form and use that for easy threading.
0
 

Author Closing Comment

by:FrancOIT
ID: 37811953
Thanks.  This fixed the problem.
0
 

Author Comment

by:FrancOIT
ID: 37811960
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
 

Expert Comment

by:-Alan-
ID: 37811979
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
 

Expert Comment

by:-Alan-
ID: 37811984
Calling DoEvents is bad practice!
0
 

Author Comment

by:FrancOIT
ID: 37811999
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
 

Expert Comment

by:-Alan-
ID: 37812012
I dunno! This was my very first post to EE!
0
 

Author Comment

by:FrancOIT
ID: 37812017
Geez.  I really appreciate your help.
0
 

Expert Comment

by:-Alan-
ID: 37812032
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
 

Author Comment

by:FrancOIT
ID: 37812053
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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question