Click here to Skip to main content
15,918,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question regarding multithreading. I currently have a project that I’m running under the main thread. Everything works well but I want to make some changes to it to make it more user friendly. The app is used to batch run files through a post processor. The user starts out by selecting a folder where all the files reside. The app then parses the folder to find files that match a certain file extension. If the file extension matches, the app, loads the file name into a Non-bound datagridview. The datagridview also contains a checkbox column that user can use to either select or deselect a certain file. The user selects all the files to run using the checkbox column and then hits the post process button to start the batch run. The program then loops through the datagridview and finds all the files that have been selected and runs them through a 3rd party post processor. After the file has been post processed, the app reads a log file created by the post processor to see if any errors occurred. After reading the log file, the app updates an “error” column in the datagridview. Upon completion, the program starts the loop over again with the next file.

Everything in the app works great except for one thing. Once the loop has started, it is impossible for the user to stop the process mid stream because everything is running on the main thread. I have done some very simple multithreading apps in the past so I tried to switch the post processor “process” over to an external thread. I was able to get the app to post process the file via the thread but when running multiple files, the app now wants to start up a process for each file in the loop. The issue I have with this is that we only have a limited number of post processor licenses so I don’t want the app to start up more than one more thread at one time. If I tell the main thread to wait until the post processor process is complete, the main thread will no longer be responsive for GUI interaction if the operator wants to stop the process. How do I set up my app to remain responsive to GUI interaction while running the post processor operation? I was thinking about moving my entire loop to the external thread but I don’t know if that is the correct direction to go or not?

1 solution


I would go for a BackgroundWorker[^].

Just create it before starting your loop ; there will only be only one thread more, on which your will use your post-processor. And your GUI will still be responsive.
Share this answer
theskiguy 23-Apr-12 16:48pm    
I am trying to switch to using a background worker but i'm having difficulties updating the UI. My UI mainly consists of an unbound datagridview and a progress bar. I was previously updating the UI after I post process each file but now I can't do that on my worker thread anymore. Any ideas on how I can update my datagridview using the processchanged event? I found examples on how to update a progress bar but I cant seem to find any on how to update a datagridview.

phil.o 24-Apr-12 11:38am    
I'm not a DataGridView specialist, but I would say you have to refresh its datasource rather than the grid itself. It depends on how you are populating it. Without further informations I can only make guesses.

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