Without going into too many details, picture a Windows form that has only one button and one muti-line textbox.
When the button is clicked, an event handler procedure retrieves text and, when it's done retrieving it, displays it on the textbox, by assigning the expression that represents the retrieved text to the textbox's Text property.
Assume that no matter how the code is optimized, it's going to take a while before all the text is retrieved. The problem is that while the text is being retrieved, the Windows form remains "frozen", and interaction with the form is temporarily blocked.
I don't want that to happen. I want the form to remain usable while the text is being retrieved, and then, when the text is retrieved, I want it to appear on the textbox, even if it takes a while before that happens.
I tried to solve this problem by spawning a thread inside the event handler procedure and letting that thread retrieve the text, but .NET throws an exception when the spawned thread tries to set the textbox's Text property or interact with any of the form's controls.
Forcing the main thread to wait for the spawned thread to finish doing its work, and then letting the main thread set the textbox's Text property with the text retrieved by the spawned thread doesn't solve the problem, because the form remains frozen while the main thread waits for the spawned thread.
I also tried including within the event handler procedure a call to the form's invoke method and passing as an argument a reference to a delegate procedure that retrieves the text, but the form remained frozen until the text was retrieved.
Can someone tell me how to solve this problem?