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

  • Home
  • SEARCH
  • 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 549027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:08:29+00:00 2026-05-13T11:08:29+00:00

I would like to use the BackgroundWorker to perform a database transaction from a

  • 0

I would like to use the BackgroundWorker to perform a database transaction from a GUI.

How can I command the BackgroundWorker to perform the work and then WAIT for the worker to complete while keeping the GUI responsive?

Do I have to use DoEvents for this purpose, or is there another way?

  • 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-13T11:08:30+00:00Added an answer on May 13, 2026 at 11:08 am

    Asking “How can I command the BackgroundWorker to perform the work and then WAIT for the worker to complete while keeping the GUI responsive?” is really asking “How do I use the BackgroundWorker?”. That’s what BackgroundWorker does.

    When you write a background task, you’re basically breaking up a method into four pieces:

    1. Getting ready to run the task.
    2. The task itself.
    3. Reporting progress to the UI while the task is running.
    4. Cleaning things up when the task is done.

    So you’re going to need to write four methods. The first is a method that creates a BackgroundWorker, adds event handlers to its DoWork, ProgressChanged, and RunWorkerCompleted events, puts the UI into whatever state it needs to be in while the task is running, and calls RunWorkerAsync to start the task.

    The other three are those three event handlers. DoWork is a DoWorkEventHandler that does the work, and that calls ReportProgress whenever it needs to report its progress to the UI. ProgressChanged is a ProgressChangedEventHandler that actually updates the UI when ReportProgress gets called. And RunWorkerCompleted is a RunWorkerCompletedEventHandler that tells the UI that the job is done.

    That’s all there is to it.

    Well, not quite all. First, you have to make sure that you check and handle the Error property in your completion handler. If you don’t do this, you will have no way of knowing that your do-work method threw an exception, since it’s not happening in the UI thread and thus doesn’t throw an exception that you can see. (It’ll show up in the Output window, if you’re looking for it.)

    Second, you have to make sure that the DoWorkEventHandler doesn’t touch anything in the UI. This can be tricky if you’re using the MVVM pattern and you haven’t planned for this contingency, because through the magic of data binding you probably have things in your model that update the views that the UI is bound to, which means that manipulating the model in your do-work method is manipulating the UI. Assuming that you’re implementing INotifyPropertyChanged, a good way to avoid trouble here is to build a mechanism whereby your views don’t raise PropertyChanged events while a background task is running.

    You also need to figure out what pieces of the UI should be disabled while the task is running. This depends on your UI. The answer might be “all of it,” and it might not. One of the things that makes using a modal form for your progress indicator attractive is that it frees you up from having to figure this out, since your entire UI is disabled while the modal form is open. If you’re disabling controls or turning off property-change notifications in the launch method, you’ll need to turn things back on again in the completion method.

    And remember: any updates that the active parts of your UI make to your data model are a potential source of cross-thread data access errors. If your UI and the background thread ever update the same object, bad things happen – they’re particularly bad if you haven’t handled the Error property in your completion handler, and the cross-thread exception kills the background task without your knowing it. If it sounds like I’m dwelling on this, it’s because I’ve lost a lot of hours of my life to doing this particular thing wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can write out the data directly to the response… May 14, 2026 at 5:36 am
  • Editorial Team
    Editorial Team added an answer I think you can use [Browsable (false)] and [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] The… May 14, 2026 at 5:36 am
  • Editorial Team
    Editorial Team added an answer So the ultimate answer I came to was to switch… May 14, 2026 at 5:36 am

Related Questions

My question pertains to multi-threading in Java. I'm translating an app I wrote in
I've been trying to smoothly animate some Windows Form location but I'm having some
I have a simple UserControl for database paging, that uses a controller to perform
I am trying to use the .NET Backgroundworker Object in an application I am
Please could you give me your thoughts on the following (especially if its advisable

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.