0

I know that regarding this problem there are many questions and also many answers, but I am reading since days and cannot find a solution for my problem. I should also mention that I am beginner in this matter.

First of all I want to explain in text what my vb.net program does:

  1. After starting my app with a button, I start a filewatcher for a status message file on the MainForm and I start a new thread for calculating my testdata.
  2. This thread initiates a class and within this class it initiates a filewatcher too.
  3. When testdata are created in the monitored folder, the thread analyses my data and writes it to a database.
  4. Within the calculation and after writing to the database the task writes some status messages to the status message file.
  5. The content of the status messages should be shown on the MainForm within a textbox and should update always when a new status was written, so that the newest status is always on top of the TextBox. Also the progress has to be reported to the MainForm in order to be shown with a progressbar.
  6. This is repeating as long as I have new testdata in my folder, in other words is my app a "data vacuum cleaner" which sucks all the data from the monitored directory in order to analyse and to write it to a database. If all the data in the folder is analysed and written to the database, it waits for new data and after the filewatcher fired a new event, the task starts analysing and writing again. So I dont start a Task always when I have new data. The task is started only when I start my app. The rest is done by the filewatcher event within this task.

I am not able to do that because I can't modify my GUI from this thread. I should also mention, that the code for this thread is in another class.

This is my code on the main form:

Private Async Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

        Await Task.Run(Sub() gFileWatcher.InitWatcher())

    End Sub

How is it possible to refresh the TextBox in realtime, I mean after status has changed?

Mark Mullen
  • 15
  • 1
  • 6
  • Possible duplicate of [How can I run code in a background thread and still access the UI?](https://stackoverflow.com/a/45571728/3740093) – Visual Vincent Feb 28 '19 at 14:29
  • In my answer in the suggested duplicate I explain how you can _generally_ access the UI from a background thread/task from within the current form/control. To be able to do this from another class you need to pass that class an instance of one of your UI elements (preferably your form) so that you can use that to invoke. – Visual Vincent Feb 28 '19 at 14:31
  • 1
    Short answer: Use `IProgress(Of T)` and `Progress(Of T)`. `Invoke` will work but ties your background code to a specific UI framework, making it much more difficult to test and reuse. – Stephen Cleary Feb 28 '19 at 15:28
  • 1
    @StephenCleary : I wasn't aware of this class. Thanks for teaching me something new today! – Visual Vincent Feb 28 '19 at 17:05

0 Answers0