Search code examples
.netuser-interfacebackgroundworker

Preventing UI lag when retrieving results from BackgroundWorker, .NET 3.5


Looking for some others thoughts on this cause I couldn't come up with anything clever.

I have a log file that I let users search with by string matching. Presumably, this log file may get huge, so I decided to run the search on a BackgroundWorker and return an array of matching strings to a RichTextBox on my main UI.

I made my log around a million lines and let it rip. At first, I thought my thread wasn't actually executing off the main process, but then I realized the search operation was extremely fast and it was updating the GUI that was taking 10+ seconds to do:

if ( e->Error != nullptr )
      {
         MessageBox::Show( e->Error->Message );
      }
      else
      if ( e->Cancelled )
      {

      }
      else
      {
          try
          {
             // This is the line that kills me
             log_textBox->Lines = dynamic_cast<array<String^>^>(e->Result);
             if(log_textBox->Lines->Length == 0)
                  log_textBox->Text = "No Matches Found.";
          }
          catch(Exception^ e)
          {

          }
      }

Loading the resultant array into the textbox is what is freezing my UI. Is there anyway to update the UI with potentially large amounts of data without appearing to be locked up?


Solution

  • The Textbox class is not meant for the display of large amounts of data (take a look at Notepad).

    Have you considered using a ListView in VirtualMode instead?