Click here to Skip to main content
15,908,254 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: TypeDesciptor.GetConverter(customtype) doesn't return the converter for the custom type Pin
leppie25-May-08 22:21
leppie25-May-08 22:21 
QuestionThumbnails with black bars Pin
dascalos21-May-08 6:49
dascalos21-May-08 6:49 
QuestionThreading question Pin
kensai21-May-08 3:51
kensai21-May-08 3:51 
AnswerRe: Threading question Pin
Laddie21-May-08 4:15
Laddie21-May-08 4:15 
QuestionRe: Threading question [modified] Pin
kensai21-May-08 5:11
kensai21-May-08 5:11 
AnswerRe: Threading question Pin
led mike21-May-08 6:08
led mike21-May-08 6:08 
GeneralRe: Threading question Pin
kensai21-May-08 21:14
kensai21-May-08 21:14 
GeneralRe: Threading question Pin
Peter Josefsson Sweden22-May-08 10:50
Peter Josefsson Sweden22-May-08 10:50 
From your problem description it sounds like led mike is right...

You say that you want to:

1. Run operation 1 on the next item in the array.

2. Wait for it to complete.

3. Run operation 2 on the same item.

4. Wait for it to complete.

5. Advance to the next item and start over at 1.

If we understand you correctly, there is no point whatsoever in running the operations on separate threads. If you need to run all of it on another thread than the main thread (so as not to block the UI), why not place the entire loop (for (i == 0; i < MyList.Length; i++) { Op1(i); Op2(i) } in a background thread?

If you instead want both operation 1 and 2 to be done on all elements in a list and the operations can be done in any order (2 before 1 or 1 before 2) on each element, run one (background) loop for operation 1 and one for operation 2. Don't bother letting them try to wait for eachother.

If you instead mean that both operations are lengthy and shouldn't block eachother (you want operation 1 to be able to proceed to the next element while operation 2 is working with the last one), but operation 1 has to happen before operation 2 on each element, try something like this:

// I assume there is a MyList with all items and methods Op1(int index) and Op2(int index) to do the work
private volatile int worker1index = 0;
private volatile int worker2index = 0;

private void Worker1()
{
    for (worker1index = 0; worker1index < MyList.Length; worker1index++)
    {
        Op1(worker1index)
    }
}

private void Worker2()
{
    for (worker2index = 0; worker2index < MyList.Length; worker2index++)
    {
        // make sure we're not ahead of Worker1:
        while (worker1index <= worker2index)
        {
            // put this thread to sleep for specified number of milliseconds until we check
            // again to see if Worker1 has caught up with us:
            Sleep(100); // adjust to a reasonably small part of Op1:s normal execution time
        }
        Op2(worker2index);
    }
}


Declaring the index variables volatile tells the compiler not to optimize away any memory accesses, and that is enough here. No need for "Interlocked" stuff (an int read or write is always required to be atomic, and even though the increment isn't it doesn't matter in this case). Sleeping in a loop is a bit crude, but it works...

Just fire up each worker on its own background thread to get the job done. Use BackgroundWorkers and their events to get signaled when they are done.

Be aware that you of course cannot add or remove items in the List while the workers are running. That is not threadsafe and will crash you.

--
Peter

AnswerRe: Threading question Pin
supercat924-May-08 14:55
supercat924-May-08 14:55 
QuestionHow do I know an assembly was created under which version of .net framework Pin
Tako.Lee20-May-08 17:41
Tako.Lee20-May-08 17:41 
AnswerRe: How do I know an assembly was created under which version of .net framework Pin
Laddie20-May-08 19:13
Laddie20-May-08 19:13 
GeneralRe: How do I know an assembly was created under which version of .net framework Pin
Tako.Lee20-May-08 20:31
Tako.Lee20-May-08 20:31 
GeneralRe: How do I know an assembly was created under which version of .net framework Pin
Laddie20-May-08 20:41
Laddie20-May-08 20:41 
GeneralRe: How do I know an assembly was created under which version of .net framework Pin
Tako.Lee20-May-08 20:52
Tako.Lee20-May-08 20:52 
GeneralRe: How do I know an assembly was created under which version of .net framework Pin
Laddie20-May-08 20:57
Laddie20-May-08 20:57 
GeneralRe: How do I know an assembly was created under which version of .net framework Pin
Tako.Lee20-May-08 21:39
Tako.Lee20-May-08 21:39 
QuestionSoftware integration has always been a big problem Pin
angels77720-May-08 16:00
angels77720-May-08 16:00 
AnswerRe: Software integration has always been a big problem Pin
angels77720-May-08 16:07
angels77720-May-08 16:07 
GeneralRe: Software integration has always been a big problem Pin
Brady Kelly21-May-08 3:05
Brady Kelly21-May-08 3:05 
QuestionDifferences between .Net Framework and .Net Compact Framework ? [Solved] Pin
Nelek20-May-08 10:44
protectorNelek20-May-08 10:44 
AnswerRe: Differences between .Net Framework and .Net Compact Framework ? Pin
led mike20-May-08 11:59
led mike20-May-08 11:59 
GeneralRe: Differences between .Net Framework and .Net Compact Framework ? Pin
Ibuprofen20-May-08 19:39
Ibuprofen20-May-08 19:39 
GeneralRe: Differences between .Net Framework and .Net Compact Framework ? Pin
Mike Dimmick21-May-08 1:23
Mike Dimmick21-May-08 1:23 
AnswerRe: Differences between .Net Framework and .Net Compact Framework ? Pin
Mike Dimmick21-May-08 1:15
Mike Dimmick21-May-08 1:15 
GeneralRe: Differences between .Net Framework and .Net Compact Framework ? Pin
Nelek21-May-08 7:15
protectorNelek21-May-08 7:15 

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.