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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:22:11+00:00 2026-05-30T16:22:11+00:00

Morning. (Attempting to do): Im tryin to execute several tasks in parallel. Each task

  • 0

Morning.

(Attempting to do): Im tryin to execute several tasks in parallel. Each task runs through a certain section of a text file using a for loop (1st task = rows 1-10. 2nd task = rows 11-20 etc). At the end of each row check, an event is raised and the GUI thread updates the UI ie “X rows have been checked.”

(Stuck on):I dont understand how to do this and yet still be able to handle exceptions correctly. As far as im aware, i need to call

task.Wait();  

or

task.WaitAll();

but by calling it, the UI thread just waits there and any events i fire inside the tasks to update the UI just get added the the UI’s queue.

Does anyone know how to get around this?

Note: I apologise in advance if this is stupid but im having some issues with threading.

as the tasks bundle all exceptions into AggregateException and if i call task.Waitall(); the UI thread just seems to wait until the tasks are completed or an exception is thrown.

  • 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-30T16:22:12+00:00Added an answer on May 30, 2026 at 4:22 pm

    One option is to attach another task to your task, as a ‘handler’ to deal with any exceptions.

    Here’s an example:

    var task = Task.Factory.StartNew(() =>
    {
        throw new Exception("oops!");
    });
    
    // attach an exception-handling task
    task.ContinueWith(previousTask =>
    {
        // do something with the exception
        Console.WriteLine(previousTask.Exception);      
    
    }, TaskContinuationOptions.OnlyOnFaulted);
    

    Note that the task attached with ContinueWith has TaskContinuationOptions.OnlyOnFaulted, which means it will only run if the task it is attached to throws an exception.

    This way, your exceptions can be handled asynchronously, and you don’t need to wait on anything.

    However, since you want to do a parallel for loop, I’d probably do something like:

    Task.Factory.StartNew(() =>
    {
        try 
        {
            Parallel.For(..., i => 
            {
                ... 
            });
        }
        catch (AggregateException e)
        {
            // handle it somehow
        }
    });
    

    This way, your main task waits on all the child tasks, but the UI thread which spawned it doesn’t have to wait.

    The main thing you have to be sure of is that your catch clause (or the handling task in previous example) never throws an exception.

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

Sidebar

Related Questions

Morning, I'm trying to split a large text file (15,000,000 rows) using StreamReader/StreamWriter. Is
This morning I ran into an issue with returning back a text string as
Good morning. I have an XML file which contains lists of warning and errors
Morning Everyone, I've been attempting to write an application that does some GETs from
I've been attempting to debug this all morning but can't seem to find the
Morning all I have frequently used the old <asp:Label ID=lblWas runat=server Text='<%# XPath(FACEVALUE) %>'></asp:Label>
Good morning, I am attempting to create a centered 3 column layout (h1, h2,
Morning all. I have the following javascript in my code in front <script type=text/javascript
Good morning stackoverflow ! I'm trying to extract from a binary file a hexadecimal
I have been attempting this all morning (VS2K10, OL2K7, .NET 3.5) and my PSTs

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.