WinForms can become quite slow and unresponsive if you add many items to a ListBox. This is particularly true if, as in your example, you do the adding from the UI thread. The UI will freeze up until the last item is added.
What can I do?
A simple improvement is to call
SuspendLayout();
before your loop starts, and
ResumeLayout();
after the loop completes. That will reduce the cost of updating the listbox, so the whole thing completes faster.
How Can I Prevent Locking the UI Entirely?
You can use threading to update the list in the background. For WinForms, BackgroundWorker is commonly used. Note that you cannot directly update the UI from the BackgroundWorker, as they run on different threads. The usual event mechanism ReportProgress
does not apply in this situation, since you want to continually update the UI until all files are loaded. Instead, you can use the approach outlined here: