Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have googled too much for this particular problem .What am trying to do is reading the multiple files from file DialogBox and I want to create number of threads based on number of files selected by user in file DialogBox i.e. if user has selected 2 threads then I should create 2 threads only and each indivisual thread should process the single file parallerly. Is this possible ?

What I have tried:

I have created a file dialog box . I am reading those files in file Dialog box and applied Lock on reading those files . But am not getting how do I run it parallely for multiple threads.
Posted
Updated 6-Feb-18 7:51am
Comments
johannesnestler 5-Feb-18 7:22am    
Yes it should be possible (if this is a "good idea" depends on your context - from my experience IO-Bound multithreading may be not so a great thing).
So what's your specific problem? creating the threads? reading the files? You didn't show any code to guess what could be your obstacle.
Anyway: From FileDialog you get a list of file-names, Loop through them create a thread for processing for each. Let them run and syncronize. I'd use TaskParallelLib (Tasks) instead of old style thread creation....
Ni!E$H_WAGH 6-Feb-18 8:22am    
What I expect is to run those threads parallely means at the same time for different files . but it gives error when I don't use Lock , error says Memory currupt.

You have to loop through the collection of files and have to create new thread for every single file you want to process.

For further details, please see:
Threading (C#) | Microsoft Docs[^]
Multithreaded Applications (C#) | Microsoft Docs[^]
Walkthrough: Multithreading with the BackgroundWorker Component (C#) | Microsoft Docs[^]
 
Share this answer
 
Comments
CPallini 5-Feb-18 7:41am    
5.
Maciej Los 5-Feb-18 7:44am    
Thank you, Carlo.
Quote:
if user has selected 2 threads then I should create 2 threads only and each indivisual thread should process the single file parallerly. Is this possible ?
I assume you meant: "if the user selects 2 files then I should create 2 threads. Each thread must process a file. Is it possible?"
Yes, it is possible of course.
I suggest you reading a tutorial on multitasking in C#.
 
Share this answer
 
I don't know your reason for specifying 1 file = 1 thread, maybe there's a good reason for it.
But if your reason is just to perform file processing in the background without encumbering the UI thread, investigate using async/await to perform the time-consuming I/O stuff.
Leaving the UI free & responsive.
 
Share this answer
 
Comments
Ni!E$H_WAGH 6-Feb-18 8:28am    
but depend on I/O.I have to process further and I think the further processing of files depends on reading of PDF files . So is that possible that I could hold reading and process on the PDF file ( which is I think not available untill I read ).
If you want to multithread I/O-dependent processes, you may want to investigate I/O Completion Ports[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900