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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:40:40+00:00 2026-05-26T08:40:40+00:00

My scenario: Windows Service .NET 4 I poll a database for entities. When new

  • 0

My scenario:

  • Windows Service .NET 4
  • I poll a database for entities.
  • When new entities come in they are added to a BlockingCollection.
  • In the service’s OnStart I create a System.Threading.Tasks.Task whose job is to enumerate the BlockingCollection (using GetConsumingEnumerable()).

The problem I’m having is this:

  • When an unhandled exception occurs in the task, I want the exception logged and the service stopped.
  • I can’t catch exceptions from the task unless I call Task.Wait().
  • If I call Task.Wait() the OnStart method blocks and the service never finishes starting.

So how can I make this work?

  • 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-26T08:40:41+00:00Added an answer on May 26, 2026 at 8:40 am

    You can handle exceptions in a task using the `.ContinueWith’ method:

    Task.Factory.StartNew(() => {
    
        // Do some long action
        Thread.SpinWait(5000000);
    
        // that eventually has an error :-(
        throw new Exception("Something really bad happened in the task.");
    
        // I'm not sure how much `TaskCreationOptions.LongRunning` helps, but it sounds 
        // like it makes sense to use it in your situation.
    }, TaskCreationOptions.LongRunning).ContinueWith(task => {
    
        var exception = task.Exception;
    
        /* Log the exception */
    
    }, TaskContinuationOptions.OnlyOnFaulted); // Only run the continuation if there was an error in the original task.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've to create a windows service in .NET that polls a db table for
Scenario I've created a windows service, but whenever I start it, it stops immediately.
Scenario I have a windows service written in C# that performs some processing based
Scenario I've got a windows service written in C#. I've read all the google
I have the following scenario: - 64bit Windows Server 2008. - 32bit .NET application
Specification C# Distributed Application. Client/Server design. Client (Winforms), Server (Windows Service), Communication via .Net
Scenario: 1) My application is a .NET 3.5 C# web app and the database
Is it possible to create a C# EXE or Windows Service that can process
This is my scenario: Web application with a self-hosted bus (publisher) Windows service with
I have a windows service (.net) that is an implementation of a custom protocol.

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.