Click here to Skip to main content
15,913,685 members
Home / Discussions / C#
   

C#

 
GeneralRe: Will Dispose() be called by GC? Pin
Shameel18-May-11 2:21
professionalShameel18-May-11 2:21 
GeneralRe: Will Dispose() be called by GC? Pin
Pete O'Hanlon18-May-11 3:18
mvePete O'Hanlon18-May-11 3:18 
GeneralRe: Will Dispose() be called by GC? Pin
Shameel18-May-11 3:41
professionalShameel18-May-11 3:41 
AnswerRe: Will Dispose() be called by GC? Pin
PIEBALDconsult18-May-11 2:49
mvePIEBALDconsult18-May-11 2:49 
QuestionOpen Auto Cad File Pin
Anubhava Dimri17-May-11 17:44
Anubhava Dimri17-May-11 17:44 
AnswerRe: Open Auto Cad File Pin
Richard MacCutchan17-May-11 21:21
mveRichard MacCutchan17-May-11 21:21 
AnswerRe: Open Auto Cad File Pin
Dalek Dave22-May-11 0:38
professionalDalek Dave22-May-11 0:38 
QuestionUsing threads to process a collection [modified] Pin
LetMeFinclOut17-May-11 15:03
LetMeFinclOut17-May-11 15:03 
I'm looking to add threading support to an application I'm working on, and am looking for advice on how to accomplish this.

I have a collection of objects which are updated through a foreach loop. Instead, I'd like to dedicate this to a series of threads, where each object is moved in for processing as the thread becomes available.

I've come up with 2 approached on how to do this.

Method 1: Using a WaitHandle
Give each thread an AutoResetEvent and rely on the WaitHandle.WaitAny() method to identify when a thread is finished so a new one can be created.
Link: MSDN[^]

Pseudocode:
void QueueThreads(IEnumerable myCollection, uint maxThreads)
{
    uint usedThreads = 0;
    AutoResetEvent[] autoEvents = new AutoResetEvent[Math.Min(maxThreads, myCollection.Count)];
    
    foreach (T myObject in myCollection)
    {
        if (usedThreads < maxThreads)
        {
            autoEvents[usedThreads] = new AutoResetEvent(false);
            //Start a DoWork() thread, passing myObject and autoEvents[usedThreads]
            usedThreads++;
        }
        else
        {
            uint freeThreadID = WaitHandle.WaitAny(autoEvents);
            usedThreads--;
            //Start a DoWork() thread, passing myObject and autoEvents[freeThreadID]
            usedThreads++;
        }
    }
    WaitHandle.WaitAll(autoEvents);
}

void DoWork(myObject, AutoResetEvent are)
{
    //Do stuff with myObject
    //Save results to file/database
    are.Set()
}


Method 2: Sharing the enumerator across threads
Give each thread access to the enumerator used to iterate over the collection. Each thread stays alive until all items in the collection have been exhausted.

Pseudocode:
void QueueThreads(IEnumerable myCollection, uint maxThreads)
{
    uint usedThreads = 0;
    IEnumerator myCollectionEnum = myCollection.GetEnumerator();
    
    for (uint i = 0; i <= Min(myCollection.Count,maxThreads); i++)
    {
        //Start a DoWork() thread, passing myCollectionEnum
    }
    //Find some way to wait until all threads are done
    myCollectionEnum.Reset();
}

void DoWork(IEnumerator myColEn)
{
    do
    {
        T myObject;
        lock (myColEn)
        {
            if (myColEn.MoveNext())
            {
                myObject = myColEn.Current
            }
            else
            {
                break;
            }
        }
        
        //Do stuff with myObject
        //Save results to file/database
    }
}

My experience with threading has been only experimental, so I'm hoping the CP threading vets can provide input on which approach is best, or if there's a third option worth considering.

As for starting a thread, I’ve seen a few different methods for doing this (ThreadPool.QueueUserWorkItem(), new Thread(new ThreadStart()), BackgroundWorker) , but don’t really know when to use which one.

Note that file/database IO would need to be synchronized between the threads, but aside from that, there shouldn't be any issued with shared resources as each object maintains its own state.

Also, I am restricted to using .Net 2.0.

modified on Thursday, September 15, 2011 2:37 PM

AnswerRe: Using threads to process a collection Pin
Not Active17-May-11 16:30
mentorNot Active17-May-11 16:30 
GeneralRe: Using threads to process a collection Pin
LetMeFinclOut17-May-11 17:13
LetMeFinclOut17-May-11 17:13 
AnswerRe: Using threads to process a collection Pin
Luc Pattyn17-May-11 18:30
sitebuilderLuc Pattyn17-May-11 18:30 
GeneralRe: Using threads to process a collection Pin
LetMeFinclOut18-May-11 0:49
LetMeFinclOut18-May-11 0:49 
GeneralRe: Using threads to process a collection Pin
Luc Pattyn18-May-11 1:59
sitebuilderLuc Pattyn18-May-11 1:59 
GeneralRe: Using threads to process a collection Pin
BobJanova18-May-11 23:16
BobJanova18-May-11 23:16 
AnswerRe: Using threads to process a collection Pin
Luc Pattyn17-May-11 16:30
sitebuilderLuc Pattyn17-May-11 16:30 
QuestionListView Pin
wizardzz17-May-11 11:21
wizardzz17-May-11 11:21 
AnswerRe: ListView Pin
gavindon17-May-11 11:24
gavindon17-May-11 11:24 
AnswerRe: ListView Pin
RobCroll17-May-11 16:36
RobCroll17-May-11 16:36 
AnswerRe: ListView Pin
Mycroft Holmes17-May-11 19:47
professionalMycroft Holmes17-May-11 19:47 
AnswerRe: ListView Pin
Shameel18-May-11 0:27
professionalShameel18-May-11 0:27 
GeneralRe: ListView Pin
wizardzz18-May-11 8:24
wizardzz18-May-11 8:24 
QuestionHow to detect mouse over and out on windows title bar Pin
Tridip Bhattacharjee17-May-11 3:43
professionalTridip Bhattacharjee17-May-11 3:43 
AnswerRe: How to detect mouse over and out on windows title bar PinPopular
Dave Kreskowiak17-May-11 4:58
mveDave Kreskowiak17-May-11 4:58 
QuestionSelecting elements from array at specific indices Pin
Chesnokov Yuriy17-May-11 1:25
professionalChesnokov Yuriy17-May-11 1:25 
AnswerRe: Selecting elements from array at specific indices Pin
Luc Pattyn17-May-11 1:54
sitebuilderLuc Pattyn17-May-11 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.