0

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 !

Ted Kon
  • 57
  • 1
  • 5
  • 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
  • 2
    This 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

1 Answers1

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.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.doevents?view=netframework-3.5

Think2826
  • 136
  • 7
  • 1
    In 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://docs.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