1

I'm a bit rusty to visual basic, however I need to use it for a project I'm working on that requires a .NET language.

I noticed when writing a bit of code to update the text value of a textbox in a windows fourm application

   'weve read the stuff...now lets update the textbox with the ADC value
    DataQSingleDevice.GetInterleavedBinaryData(BinaryData, 0, 18)



    DataQSingleDevice.Stop()
    DATAQHandler(0).Disconnect()




    'now lets throw data in the textbox
    Button1.Text = "Connected!"

    For incramenter As Integer = 0 To 10
        TextBox1.Text = BinaryData(incramenter)
        Threading.Thread.Sleep(2000)
    Next
    end sub

that when I go through this for loop above me, it doesn't update the text value at every iteration. I assume that means that it can only do it after the method that this sub is in has finished.

I remember from Android programming in Java that property modifications like this usually implement on the main UI thread buried super deep inside a never ending for loop that only GOD himself and the inventor of the java language could ever hope to find. I also remember that methods like AsyncTask<> and Java.util.concurrent allowed me to do things on a background thread and update certain views.

My question:

Is there a way to update the property of things in visualBasic on a GUI such as "TextBox1.Text" similar to how some views in Android programming can be periodically updated with background threads? (this process may span a couple minutes of updating...this example only lasts 20 seconds but my practical usage could last 10 minutes)

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 2
    If this is running in a background thread, you'll have to call BeginInvoke to force the update to happen on the UI thread. – dwilliss Oct 11 '17 at 18:08

1 Answers1

1

Actually, there is a proper way to access the UI from another thread. You should do this:

TextBox1.Invoke(Sub() TextBox1.Text = BinaryData(incramenter))

or

TextBox1.BeginInvoke(Sub() TextBox1.Text = BinaryData(incramenter))

Instead of this:

TextBox1.TextBox1.Text = BinaryData(incramenter)

The difference between .Invoke and .BeginInvoke is that the first will run synchronously, that is, the current thread will wait the action inside the lambda sub to be fully performed in UI thread, and only then will go on. The latter will send the lambda sub to be performed in UI thread without waiting it to run, so the caller thread proceeds immediately. Its up to you to choose the one which suits you better.

VBobCat
  • 2,527
  • 4
  • 29
  • 56