Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:59:59+00:00 2026-05-26T15:59:59+00:00

Consider this example: Private Sub Button_Click( sender As Button, e As RoutedEventArgs) Handles btn.Click

  • 0

Consider this example:

Private Sub Button_Click(
    sender As Button, e As RoutedEventArgs) Handles btn.Click

  sender.IsEnabled = False

  Thread.Sleep(5000)

  sender.IsEnabled = True
End Sub

In my scenario the Button_Click is a command delegate in the VM, and the Thread.Sleep is some long-running process (about 2-10 seconds).

I want, that when the user calls the command, it should immediately update the UI disabling the button so the user cannot execute it while it’s running, then execute that operation, then, when operation completed, unblock the button.

I tried wrapping the middle line like the following:

Dispatcher.BeginInvoke(Sub() Thread.Sleep(5000))

But it didn’t do the job.
What’s the best way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:59:59+00:00Added an answer on May 26, 2026 at 3:59 pm

    Instead of creating a thread of your own you can also use the BackgroundWorker Control.
    By calling the Method “RunWorkerAsync” the DoWork Event get’s called in another Thread.

    By Calling the Method “CancelAsync” form your UI thread you can set the Backgroundworker to “Cancellation Pending” (Property of the Control “CancellationPending” is then true). In your long running background thread you can check for that property (e.g. if you have a loop: exit the loop as soon as CancellationPending is true). This is a quite nice feature to safely abort the thread.

    In addition with the Backgroundworker you can also report the progress of the thread (e.g. for use in a ProgressBar)

    Example:

    Public Class Form1
    
       Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
          '** Set to true if you want the ReportProgress Event
          BackgroundWorker1.WorkerReportsProgress = True
          BackgroundWorker1.WorkerSupportsCancellation = True
       End Sub
    
       Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
          Dim i As Integer
          Dim n As Integer = 100
          Dim iLastPerc As Integer
    
    
          While Not BackgroundWorker1.CancellationPending AndAlso i < n
    
             '** Do your time consuming actions here
             Threading.Thread.Sleep(500)
    
             If Math.Floor((i / n) * 100) > iLastPerc Then
                '** If the Progress has changed. Report
                iLastPerc = CInt(Math.Floor((i / n) * 100))
                BackgroundWorker1.ReportProgress(iLastPerc)
             End If
    
             i += 1
          End While
    
       End Sub
    
       Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
    
          '** Run the Backgroundworker
          BackgroundWorker1.RunWorkerAsync()
    
       End Sub
    
       Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    
          '** Update the ProgressBar
          ProgressBar1.Value = e.ProgressPercentage
    
       End Sub
    
       Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    
          '** Worker is done. Check for Exceptions or evaluate the Result Object if you like
    
       End Sub
    
       Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
    
          '** Cancel the worker
          BackgroundWorker1.CancelAsync()
    
          MsgBox("Finished!")
    
       End Sub
    End Class
    

    In reference to your question the code should be:

    Private Sub btn_Click(sender As Button, e As RoutedEventArgs) Handles btn.Click
      sender.IsEnabled = False
      Using bw As New BackgroundWorker()
        AddHandler bw.DoWork, Sub(s, ea) Thread.Sleep(5000)
        AddHandler bw.RunWorkerCompleted, Sub(s, ea) sender.IsEnabled = True
        bw.RunWorkerAsync()
      End Using
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this example: public class Factory { private List<ISubFactory> subFactories; public Factory(List<ISubFactory> subFactories) {
Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private
Consider this example. public class myclass { public void... public int ... private class
Please consider this example. (PHP) class Database{ private $result; private $conn; function query($sql){ $result
Let's consider this example : public class Shared { private int attribute; public Shared()
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.