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 1002851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:53:16+00:00 2026-05-16T07:53:16+00:00

I have a rather simple threading question. I’m writing a simple utility that will

  • 0

I have a rather simple threading question.

I’m writing a simple utility that will run various SQL scripts based on parameters defined by the user.

In order to keep the UI responsive and provide feedback as to the status of the scripts that are being executed, I’ve decided that using ThreadPool.QueueUserWorkItem would be appropriate to handle the execution of the various scripts (via SMO.)

However, I’m a bit confused as to how I can relay the output information that SMO will return back to the UI thread.

For this utility, I’m using WPF and MVVM for the presentation. I’m thinking that I would have a ScriptWorker class that I could pass the parameters and locations and order in which to run the scripts to.

After I run each script, I’d like to somehow return the results to the UI thread so that it updates the output window and then I’d like for the worker to move to the next task.

I’m certain this is a basic question, but after looking at QueueUserWorkItem and seeing that I essentially start the work through a callback, I’m unsure how I’d accomplish what I’d like to accomplish.

I’m basing my assumptions off of this Microsoft article:

http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80).aspx

Thanks for the info!

  • 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-16T07:53:17+00:00Added an answer on May 16, 2026 at 7:53 am

    QueueUserWorkItem would technically work, but is extremely low-level. There are easier ways.

    I recommend using the new Task feature of .NET 4.0. It does exactly what you want, including synchronizing the result or error conditions to another thread (the UI thread, in this case).

    If .NET 4.0 is not an option, then I’d recommend either BackgroundWorker (if your background processing is not too complex), or asynchronous delegates such as Hans mentioned. If you use async delegates, then use the AsyncOperation class to marshal the results back to the UI thread.

    The Task option is very nice because it handles parent/child tasks very naturally. BackgroundWorker can’t be nested. Another consideration is cancellation; Task and BackgroundWorker have built-in support for cancellation, but for async delegates you’d have to do your own.

    The only place where Task is a bit more complex than BackgroundWorker is in progress reporting. It’s not quite as easy as BackgroundWorker, but I have a wrapper on my blog to minimize that.

    To summarize, in order of preference:

    1. Task – supports proper marshaling of errors, the concept of a result, cancellation, and parent/child nesting. Its one weakness is that progress reporting isn’t simple (you have to create another Task and schedule it to the UI thread).
    2. BackgroundWorker – supports proper marshaling of errors, the concept of a result, cancellation, and progress reporting. Its one weakness is that it doesn’t support
      parent/child nesting, and that limits its usage in APIs, e.g., for a business layer.
    3. Delegate.BeginInvoke with AsyncOperation – supports proper marshaling of erros, the concept of a result, and progress reporting. However, there is not a built-in concept of cancellation (though it can be done by hand using a volatile bool). It also does not support parent/child nesting.
    4. Delegate.BeginInvoke with SynchronizationContext – this is the same as option (3) except it uses SynchronizationContext directly. The code is slightly more complex, but the tradeoff is that parent/child nesting is supported. All other limitations are identical to option (3).
    5. ThreadPool.QueueUserWorkItem with AsyncOperation or SynchronizationContext – supports the concept of progress reporting. Cancellation suffers from the same problem as option (3). Marshaling of errors is not easy (in particular, preserving the stack trace). Also, parent/child nesting is only possible if the SynchronizationContext is used instead of AsyncOperation. Furthermore, this option does not support the concept of a result, so any return value(s) need to be passed as arguments.

    As you can see, Task is the clear winner. It should be used unless .NET 4.0 is not an option.

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

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • 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 HttpContext.Items is a per-request store. If you don't already have… May 16, 2026 at 3:22 pm
  • Editorial Team
    Editorial Team added an answer That is the correct URL when you use map.resources rather… May 16, 2026 at 3:22 pm
  • Editorial Team
    Editorial Team added an answer Try adding the :visible pseudo-selector: $("#tableid tr:visible:even").addClass("evenClass"); Demo: http://jsfiddle.net/gRyFx/1/ May 16, 2026 at 3:22 pm

Trending Tags

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

Top Members

Related Questions

Dear scholars, code gurus. My question is rather simple, I have been searching and
I have just got the following bloody MDA exception on a rather simple little
A rather simple question really. I'm working on a project where I need to
I have a rather large, dynamic sparse matrix object class to write, and I
I'm looking to write a quick benchmark program that can be compiled and run
I have a PropertyGrid in my application that is used for editing arbitrary objects.
I have an interface in CLR between SQL Server and the Exchange Web Services
A question yesterday about doubled-checked locking started a chain of thoughts that left me
I am writing a software that requires me to prepare statements and set the
I am new to network programming but old to Python. I have a simple

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.