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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:53:36+00:00 2026-06-08T15:53:36+00:00

I am trying to launch a thread when an application starts and wait for

  • 0

I am trying to launch a thread when an application starts and wait for UI to give it some work without using BackgroundWorker. This thread sleeps when no work is given, and wakes up when the ui asks it do something.

More details:

Simple WPF App: I have a StorageClass to copy files to long term storage. This class is part of the WPF application. When a user clicks a button to store the file to long term storage(Low speed array), I want a thread to copy this file from a highspeed storage array to long term storage. These are large files and I dont want the UI to be blocked. I would like to use one thread that waits for instruction to transfer. Hope this gives more clarity on what I am trying to do.

  • 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-06-08T15:53:37+00:00Added an answer on June 8, 2026 at 3:53 pm

    If you are using .NET 4.0 or above, I’d recommend the TPL over threading directly, and I’d recommend using a BlockingCollection<T> as a way that the UI can “give some work” via the collection for the consumer to do.

    The consumer can then be a long-running Task (again, from the TPL) that consumes from the BlockingCollection<T>, in this way, you don’t need any sleep or manual artifacts like that, the BlockingCollection<T> lets you block on a wait for a given period of time, then resume once an item is ready to consume.

    So you could define a cancellation token source, blocking collection, and task as:

        private BlockingCollection<Transaction> _bin = new BlockingCollection<Transaction>();
        private CancellationTokenSource _tokenSource = new CancellationTokenSource();
        private Task _consumer;
    

    And your consumer method could be defined as:

        private void ConsumeTransactions()
        {
            // loop until consumer marked completed, or cancellation token set
            while (!_bin.IsCompleted && !_tokenSource.Token.IsCancelRequested)
            {
                Transaction item;
    
                // try to take item for 100 ms, or until cancelled 
                if (_bin.TryTake(out item, 100, _tokenSource.Token)
                {
                    // consume the item
                }
            }
        }
    

    Then you’d fire off the task when your form loads by doing:

    // when you have a task running for life of your program, make sure you
    // use TaskCreationOptions.LongRunning.  This typically sets up its own
    // dedicated thread (not pooled) without having to deal with threads directly
    _consumer = Task.Factory.StartNew(ConsumeTransactions, _tokenSource.Token, 
                                      TaskCreationOptions.LongRunning, TaskScheduler.Default);
    

    And then add items by doing:

            _bin.TryAdd(someTransaction);
    

    Where Transaction is just whatever you define your unit of work to perform…

    Then, finally, when your application wants to shut down, it can do:

    _bin.CompleteAdding();
    

    Which tells the consumer that no more items will ever be added to the queue, this will make the TryTake() return false, and exit the loop since _bin.IsCompleted will then be true.

    Your long running task can then loop on the blocking get until the cancellation token (also TPL) is set to tell it to shut down…

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

Sidebar

Related Questions

I'm trying to launch a WPF application from a Console application, using Application Domains,
I am trying to accomplish something like this: thread t; // create/initialize thread t.launch();
I am trying to launch my hello world application in emulator of android. But
I am trying to launch a XPage(web) mobile application from a email(Lotus traveller). When
I'm trying to launch a website url in a new tab using python in
I am trying to launch MS Office through WINE using a Perl script hosted
I'm trying to launch another application from C# application, is there a way to
I've been trying to use Java's ProcessBuilder to launch an application in Linux that
I am trying to launch a background thread to retrieve XML data from a
In my application, some process that is started in AWT's Event Dispatching Thread (EDT)

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.