I have a C# .NET Framework application that calls an unmanaged C++ DLL via DLLImport
, to parse a bunch of files given by the user and perform some operations.
I would like to have a progress bar on my C# application, as this file parsing might be a long process. The best way I have thought to do this is to parse a couple of files at a time, and then return to the C# code so I can update the progress bar.
However, this requires me to allocate some memory on the heap of the C++ DLL so I don't have to pass all the 10,000+ file paths as arguments each time I have to call the C++ function again.
I am not sure if closing the DLL after I have completed my parsing is possible. Is there any other way I can accomplish this task without having to keep all of the paths in memory allocated for the entirety of the time that the application is running?
Since you've confirmed that files are independent, you should try parallelize the operation.
On the producer side of the process, set up a List or an array with file names. If the list gets too big (really, REALLY big so it stresses available RAM), you may want to replace it with a combination of BlockingCollection and a Queue, so producer can be throttled until the number of items in the processing pipe is below threshold.
On the other side of this pipe, start a worker (inline or as separate worker thread) which will either sequentially or .AsParallel() read items from the collection, and pass them over to the unmanaged library for processing.
Since this is a UI application, I guess your worker thread should be separate. Upon each processed file, do an .Invoke() on the UI form to update the progress bar.