Hi i have a Sub that has multiple if statements in it. Each if statement has a large loop that searches for specific files and text inside files. I tried various ways to use a text box in order to get the information which if is currently proccessing at the time and i see that for some reason the ui is not refreshed until the sub finishes and so i see everytime in the textbox the last proceesed if message. What do you think is the best way to handle it? I hope that this has nothing to do with threads because threads are something that i am not familiar with !
            Asked
            
        
        
            Active
            
        
            Viewed 127 times
        
    0
            
            
        - 
                    This is because you are performing the operation on the UI thread. You need to separate the longer running operation into it's own task or thread. I'll just find a decent guide for you, one moment – Martin Jul 06 '21 at 07:59
- 
                    This article outlines the concept reasonably well: https://social.technet.microsoft.com/wiki/contents/articles/33280.vb-net-invoke-method-to-update-ui-from-secondary-threads.aspx – Martin Jul 06 '21 at 08:00
- 
                    2This is also a useful guide to using a BackgroundWorker to achieve a similar outcome: https://thedeveloperblog.com/vbnet/backgroundworker-vbnet – Martin Jul 06 '21 at 08:02
2 Answers
0
            
            
        I think using Application.DoEvents() is an easy choice. But I don't know if that would be the desired behavior. If the use of Application.DoEvents() fails, another thread should handle it.
 
    
    
        Think2826
        
- 201
- 1
- 7
- 
                    2In general, it is recommended not to use `DoEvents()` apart from in specific circumstances. See [here](https://stackoverflow.com/a/5183623/2169762) and the caution box at the end of [this](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.doevents) – Martin Jul 06 '21 at 08:14
- 
                    @Martin as i understand i have to use such backround workers as much are my if statements right ? – Ted Kon Jul 06 '21 at 08:40
- 
                    @TedKon I would suggest that any non-UI code should be separate (although this will sometimes depend upon the delay to run that code). A background worker is one way of achieving it. Or a Thread, or a Task, and there are others – Martin Jul 06 '21 at 08:53
0
            
            
        I use this:
Public Sub logWithCrLf(tx As TextBox, s As String)
    tx.AppendText(s & vbCrLf)
    tx.Select(tx.TextLength - 1, 0)
    tx.ScrollToCaret()
    tx.Refresh()
End Sub
I see that it scrolls a bit smoother using tx.AppendText(s) than tx.Text &= s, which scrolls up to 0 then down to caret again.
(I write this as an answer to contribute with the tx.AppendText() recommendation)
 
    
    
        Ivan Ferrer Villa
        
- 2,129
- 1
- 26
- 23
- 
                    1`AppendText` already scrolls by itself, so you don't need the three lines below it. Do favor `Environment.NewLine` over `vbCrLf` – LarsTech Jul 18 '22 at 19:50
