Results 1 to 4 of 4

Thread: Split Function Freezing UI - Some Backgroundworker Help Please

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    12

    Split Function Freezing UI - Some Backgroundworker Help Please

    Hi,

    I'm currently running multiple For Each loops which are time consuming loops. They started to cause issues so I added them to a backgroundworker, not only for the threading advantages but also the progress bar updating. However the below Split function is still causing the UI to freeze as it takes about 10 seconds to perform the split and the progress bar in the For Each loop that follows does not show the user that something is happening. Is there a way to stop the function from causing the freeze or making it update the progress bar?

    Code:
    lines = Split(My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName), vbNewLine)
    For Each line In lines
        'More code
    next
    I know I can use a string as apposed to the lines array, update the backgroundworkers and loop the file line by line like this...

    Code:
            processLength = File.ReadAllLines(OpenFileDialog1.FileName).Length
            For Each SplitLine As String In File.ReadAllLines(OpenFileDialog1.FileName)
                progressbarLabelText = "Processing data..."
                loopProcessCount += 1
                bwAMC.ReportProgress(((loopProcessCount / processLength) Mod 100) * 100)
                tempLine = tempLine & SplitLine & vbNewLine
            Next
    But doing so means I would have to change all my code that uses the lines array. Something I hope to avoid.

    Any suggestions would be greatly appreciated.

    Regards,

    S

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Split Function Freezing UI - Some Backgroundworker Help Please

    Consider this function rather than ReadAlltext plus a split:

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    That may do the same job faster. Split would tend to be slower.

    However, I think you already have a problem because you are accessing the OpenFileDialog, which I believe will tie you to the UI process. OpenFileDialog doesn't do all that much other than give you a friendly means to choose what amounts to a string (a full path, but still just a string). So, get that string from OpenFileDialog in the UI and put it in a form level variable. Then, in the background process, use that string rather than OpenFileDialog.
    My usual boring signature: Nothing

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Split Function Freezing UI - Some Backgroundworker Help Please

    (Consider what Shaggy Hiker posted also, I got caught up in a different thing that might be there.)

    Well, I don't really see enough code here to definitively say what's happening. But if the "more code" comment looks like your 2nd code sample, that's a hint.

    The UI thread freezes when you ask it to do more than 1 second of work within 1 second. It unfreezes once it's able to do less than 1 second of work per second. So it's obvious why the code needs to go on a background thread.

    But think about how often a loop that goes over each line of the file will execute. It'll probably happen many hundreds of times per second. Now think about how long it takes to update the UI in response, probably 2-3ms. That means if you're reading only about 300-500 lines per second, you start asking for more than 1s of UI updates per second. That's when the UI freezes. Even if you're close, more like 900ms per 1s, that doesn't leave a lot of time for user interaction and it's likely to look frozen anyway.

    So when you're updating the UI from a background thread, it's often worth writing code to try and 'throttle' updates to happen less frequently. I feel like once per second is adequate, other people get excited about 4-5 times per second. A simple way to do that would look something like this:
    Code:
    Dim lastUpdate As DateTime = DateTime.Now
    For Each line in lines
        ' the code to do whatever
    
        Dim timeDelta As TimeSpan = DateTime.Now - lastUpdate
        If timeDelta > TimeSpan.FromSeconds(1) Then
            ' Update the UI
        End If
    Next
    
    ' Remember to do one last progress update for 100%, since it might not have happened
    ' in the loop.
    This could be the solution, but I need to see more code to be sure.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Split Function Freezing UI - Some Backgroundworker Help Please

    Why would you have to change all your code that uses the 'lines' array? Why wouldn't you simply change this:
    vb.net Code:
    1. lines = Split(My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName), vbNewLine)
    to this:
    vb.net Code:
    1. lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
    Any code that uses 'lines' doesn't care how it's populated.

    That said, if you have a large file then it's still going to take time to read it all in and split it into lines, whether you do it yourself or you let the Framework do it for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width