Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Asynchronous Task with argument

Status
Not open for further replies.

Kostarsus

Programmer
Nov 30, 2000
86
DE
Hello,

I want to create asynchronous tasks in a foreach-loop.
The algorythm takes the first six values of the foreach-loop correctly. But than the task factory sets the rest of the asynchonous tasks on Wait-Status. The foreach-loop continues, so that when the next asyncronous tasks start, the value of the foreach-loop isn't right and I have a lot of tasks, which will take the same value.
In my opinion the right way is, to call the asynchronous task with the value of the foreach-loop. Can anyone help me, in the best with an example.

Here is the code, which I use, until now:
Code:
            foreach (WFileInfo fileInfo in fileInfos)
            {
                mp3InfoReadertasks.Enqueue(Task.Factory.StartNew(()=>
                    {
                        try
                        {
                            log.DebugFormat("ReadMP3_Info: path={0}, filename={1}", fileInfo.Path, fileInfo.Filename);
                            WMP3FileInfo newInfo = mp3Reader.readFile(fileInfo);
                            mp3Infos.Add(newInfo);
                        }
                        catch (Exception ex)
                        {
                            errors.Add(new WSynchronizeError() { Path = fileInfo.Path, Filename = fileInfo.Filename, Message = ex.Message });
                        }
                    }));
            }

Thanks in forward
Kostarsus
 
this error makes sense with the current code. fileinfo is scoped to the loop. when you execute the task it's using the current fileinfo in the loop, which is now out of scope.

to resolve this you need to pass the fileinfo object to task action as a argument. then the fileinfo becomes scoped to the task itself. not the loop.

on top of that there are other problems
1. the scope of mp3Infos
2. the scope of mp3Reader
3. the scope of errors
4. exception handling is general

these additional objects must also be passed to the task before invoking the task or you will have the same problems. I think a better approach would be to have mp3Reader read the file using TPL and then add them to the mp3Infos collection when the task completes.

let TPL manage the exception handling for you and then process the exception when the task is completed.

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
one other thing to note about this. if you were to pass mp3Infos into the task, you would most likely need to use InterLock.Add() when adding an item to the collection. this would prevent concurrent additions to the collection.

it other words it prevents the situation where 2 items are added to the same index on the collection.

multithreading is one of the most difficult concepts to master in any OOP. the TPL helps alleviate some of this pain, but it doesn't remove it all together.

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Hi,

thanks for the hints.
The variables of mp3Info and errors are only filled, by the tasks, so I don't think that there is a problem to use the variables. Bu you are right, I will establish a lock on these variables, when I add an item.

In the first attempt, I tried a Parallel.Foreach loop. But in the documentaion is written, that the code could executed parallel. But in the parctise, I didn't see a difference to a non-parallel execution. My six-core processor load was only 8-10 percent. Even when the thread amount were increased.
With the code above, the processor load was round about 70%. The code was executed quite faster, too.

Could you give me an example, how to pass the fileInfo object as a task argument? I found only examples without an argument.

In reguards
Kostarsus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top