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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:53:20+00:00 2026-06-06T19:53:20+00:00

I am trying to run the for loop in a separate thread so that

  • 0

I am trying to run the for loop in a separate thread so that the UI should be responsive and the progress bar is visible.

The problem is that I don’t know how to do that :). In this code, the process starts in a separate thread, but the next part of the code is executed at the same time. The messageBox is displayed and the results are never returned (e.g. the listbox’s selected index property is never set).

It doesn’t work even if I use, “taskEx.delay()”.

            TaskEx.Run(() =>
                {
                    for (int i = 0; i < sResults.Count(); i++)
                    {

                        if (sResults.ElementAt(i).DisplayIndexForSearchListBox.Trim().Contains(ayaStr))
                        {
                            lstGoto.SelectedIndex = i;
                            lstGoto_SelectionChanged(lstReadingSearchResults, null);
                            IsIndexMatched = true;
                            break;
                        }
                    }
                });

        //TaskEx.delay(1000);
        if (IsIndexMatched == true)
            stkPanelGoto.Visibility = Visibility.Collapsed;
        else //the index didn't match
        {
            MessagePrompt.ShowMessage("The test'" + ayaStr + "' does not exist.", "Warning!");
        }

Could anyone please tell me how can I use multi-threading with a “for” or “foreach” loop?

  • 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-06T19:53:22+00:00Added an answer on June 6, 2026 at 7:53 pm

    TaskEx – are you using the async targeting pack for .NET 4.0?
    In that case, use await TaskEx.Run(...) – C# 5.0 await will wait until the task is completed, but will keep the UI responsive while it is waiting (unlike the .Wait() method).

    Also, you need to move the lstGoto.SelectedIndex = i; assignment out of the background thread – accessing UI controls is only allowed on the main thread.

        int index = await TaskEx.Run(() =>
                {
                    // ensure that sResults is a List<T> - call .ToList() if necessary
                    for (int i = 0; i < sResults.Count; i++)
                    {
    
                        if (sResults[i].DisplayIndexForSearchListBox.Trim().Contains(ayaStr))
                        {
                            return i;
                        }
                    }
                    return -1; // nothing found
                });
    
        //await TaskEx.Delay(1000);
        if (index >= 0)
        {
            stkPanelGoto.Visibility = Visibility.Collapsed;
            lstGoto.SelectedIndex = i;
            lstGoto_SelectionChanged(lstReadingSearchResults, null);
        }
        else //the index didn't match
        {
            MessagePrompt.ShowMessage("The test'" + ayaStr + "' does not exist.", "Warning!");
        }
    

    A last word of warning: do not use Count()/ElementAt() repeatedly in a loop – those LINQ methods might end up traversing the whole collection to compute their result. If you need to traverse an IEnumerable by index, it’s much faster to convert it into a List once and then traverse through the list.

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

Sidebar

Related Questions

I'm trying to create a thread that configures a run loop to run a
I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However
I am trying to schedule NSTimer on a separate thread and this is how
I'm trying to run a basic loop that will find a specific value in
This is probably a newbie's escaping problem. I'm trying run command in a for
I am trying to perform some work in a separate thread and stop that
I am trying to build an application that must manage its own run loop.
I am trying to create a UDP listener that will listen on a separate
I'm trying to create a loop that will output multiple where statements for a
I've run into an interesting problem in an AI project of mine. I'm trying

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.