I have a fairly intensive application, which I’ve just converted from a console application to a Windows Form application. The program essentially loops through a couple of thousand of customers, and creates invoices based upon their deliveries. For every delivery, and customer, information would be output to the console window – which means there’s a lot of information being output! The old process took up to 20 mintues, which was reasonable given the scale of what it was doing. But my manager, and the customer, wants it as a Windows Form now.
I’ve implemented this, and to output the information I cheated a bit and simply replaced all Console.WriteLine() calls in the existing code to call a method of mine, which appends the given text to a TextBox on the Windows Form.
I’ve had 2 problems with this:
1: The textbox will occasionally ‘freeze’ and go white – but I assume I can’t use more than 1 thread, because I’ll be periodically calling the textbox on a different thread
2: More seriously, the 20 minute process of before now takes significantly longer – around 4-5 hours. I’m certain that the dataset hasn’t been enlarged dramatically, as to test all code we roll back to an old position, and the only issue i can think of is me appending to a textbox. I’ll be testing without any textbox writes tomorrow to confirm this, but the code hasn’t changed in any other way
Is there any other, easier methods to output text to a Windows Form, multiple times a second?
Your best bet here is to NOT output all of the data straight into a text box.
Instead spin up a second thread to handle the processing. Every so often (20 records? 100?) sync that thread with the main one to update some type of progress bar.
Have the thread write the normal text updates to either a database or a text file.
Once it has completed, load that text file and display to the user.
Nearly all of the extra time is spent with the application’s main thread redrawing the form; which is an expensive process. When it does this the processing is basically blocked (same thread). Because the amount of data in the textbox grows, the amount of time it spends doing the update increases linearly with each append to your textbox… So, 10 records won’t notice much of an impact. 1000+ will.. depending on how much data you are appending.