Thread: How can this WinForm functionality be implemented?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    How can this WinForm functionality be implemented?

    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?
    Last edited by y99q; 02-27-2012 at 10:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winform in c++ (or maybe c#) or WinAPI
    By gilit2 in forum Windows Programming
    Replies: 6
    Last Post: 02-06-2011, 06:06 PM
  2. Stopping Winform Process
    By darren78 in forum C# Programming
    Replies: 2
    Last Post: 10-16-2010, 10:55 AM
  3. WaitForExit(); Works, but C# WinForm disappears.
    By Dragoon_42 in forum C# Programming
    Replies: 7
    Last Post: 01-19-2010, 03:01 PM
  4. Replies: 7
    Last Post: 09-24-2006, 10:13 AM
  5. Write to Console from a WinForm
    By ginoitalo in forum C# Programming
    Replies: 2
    Last Post: 10-19-2003, 06:20 PM