Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I limit the I/O of my C# app

I built an app that performs work on thousands of files, then writes modified copies of these files to the disk. I am using a ThreadPool but it was spawning so many threads the pc was becoming unresponsive 260 total), so i changed the max from the default of 250 down to 50, this solved that issue (app only spawns about 60 threads total), however now that the files are becoming ready so quickly, its tying up the UI to the point where the pc is unresponsive.

Is there a way to limit the amount of I/O - i mean, i like using 50 threads to perform the work on the files, but not 50 threads writing at the same time when they are processed. I would rather not re-architect the writing of the files part if i can keep from it - i was hoping i could limit the amount of I/O (simultaneous) the threads from this pool could consume.

like image 871
schmoopy Avatar asked Aug 04 '10 17:08

schmoopy


1 Answers

Use a semaphore to limit no. of threads wanting to write to disk simultaneously.

http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

Limits the number of threads that can access a resource or pool of resources concurrently.

like image 94
Muhammad Hasan Khan Avatar answered Sep 29 '22 01:09

Muhammad Hasan Khan