How can I use multi-threading with a "for" or "foreach" loop?

Posted by saafh on Stack Overflow See other posts from Stack Overflow or by saafh
Published on 2012-07-01T08:50:44Z Indexed on 2012/07/01 9:15 UTC
Read the original article Hit count: 190

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading