jeudi 21 mai 2015

c# multithreading list pattern advice

I have made an application which also contains a folder/file scanner. I'm coming across a problem with the threading structure.

How it works: For each folder/file it finds it starts a thread. There is a function inside each thread that uses a list to check if a similar item has been found so that it can add to the existing item. If it's not found it will add the item to the earlier mentioned list. The threads are executed parallel (async).

Problem: Because it's async it will sometimes fail on the listcheck. This is caused because there is a time period between the check and adding to the list. Something that can happen is that the check returns that there is not a similar item, while there certainly is. This will result in the same item occurring in the list.

I have also made it that threads wait on each other. I really like the effect this gives it on the frontend. (items nicely adding to the list real time). But this takes way to long for a lot of folders/files.

Now I'm thinking of making a mix between the functions, but i would really like to see a combination of the speed of async threads and the safety of waiting on each thread.

Anybody any idea? Thanks. (sorry if it's written in bad english)

Aucun commentaire:

Enregistrer un commentaire