Invoke Method To Update UI From Secondary Threads In VB.NET

Introduction

As many developers well knows it's not possible -- using .NET Framework -- to realize a direct interaction between processes which run on threads different from the one on which the UI resides (typically, the main thread of the program). We are in a situation from which we can exploit the many advantages of multi-threading programming to execute parallel operations, but if those tasks must return an immediate graphical result, we won't be able to access the user controls from those processes.

In this brief article, we'll see how it can be possible, through the Invoke method, which is available to all controls through the System.Windows.Form namespace, to realize such functionality in order to execute a graphic refresh and update through delegates.

Delegates

The MSDN documentation states delegates are constructs which could be compared to the pointer of functions in languages like C or C++. Delegates incapsulate a method inside an object. The delegate object could then be passed to code which will execute the referenced method, or method that could be unknown during the compilation phase of the program itself. Delegates could be EventHandler instances, MethodInvoker-type objects, or any other form of object which ask for a void list of parameters.

Here follows a pretty trivial, though effective, example of their use.

Basic example

Let's consider a WinForm on which will reside a Label, Label2. That label must be use to show an increasing numeric counter. Since we desire to execute the value increase on a separated thread, we will incur into the named problem. Let's see why. First of all, we will write the code that will execute the increment of our numerical value on a secondary task from the main one, trying to update Label2, to observe the result of the operation.

  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
  2.   Dim n As Integer = 0  
  3.    
  4.   Dim t As New Task(New Action(Sub()  
  5.                               n += 1  
  6.                 Label2.Text = n.ToString  
  7.      End Sub))  
  8.  t.Start()  
  9. End Sub 

At runtime, the raised exception will attest what we saw up to here: it's not possible to modify an object properties (in reality, some of them), if the object itself is managed from a different thread other than the main one.

Yet, the Label control has - like any other control - a method named Invoke, through which we can call delegates toward the main thread. We can rewrite our method like the following. This time, for the sake of completeness, inserting our increment in a loop, to show how Invoke can work inside loops too.

  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
  2.   Dim n As Integer = 0  
  3.    
  4.   Dim t As New Task(New Action(Sub()  
  5.   For n = 1 To 60000  
  6.    
  7.   Label2.Invoke(Sub()  
  8.            Label2.Text = n.ToString  
  9.            End Sub)  
  10.   Next  
  11.  End Sub))  
  12.   t.Start()  
  13. End Sub 

Running the program we can see how the graphical data update will be correctly executed, simultaneously with the development of the numerical variable.

That's -- as aforementioned -- a very basic and trivial example, but in a delegate context it's possible to execute an arbitrary number of operations of different complexity, making it possible to realize any feature in regarding of cross-threading operations.

Update UI from different tasks

To explore the subject further, we'll see now a more demanding example, in terms of resources needed. In this case, we'll make a program that will process simultaneously many text files, to update the UI to show what a single thread is doing in a given moment.

Example definition

We have 26 different text files, each of which contains a variable number of words, mainly italian but not exclusively. Those words begin with a particular letter: for example, the file "dizionario_a.txt" will contain only words which stars with "a", "dizionario_b.txt" only those which starts with "b", and so on. We want to write a program which possesses 26 labels and, starting a task for each letter, will proceed in inserting every read word in a variable of type List(Of String). Each task must show the processed word, so that every thread will execute -- through the Invoke() method -- the refreshing of the content of the Label on which it works.

Source code

Let's take a look to the code, to make some considerations after it.
  1. imports System.IO  
  2.    
  3. public class Form1  
  4.    
  5.     Dim wordList As New List(Of String)  
  6.    
  7.     Public Sub AddWords(letter As Char, lbl As Label)  
  8.         Using sR As New StreamReader(Application.StartupPath & "\text\dizionario_" & letter & ".txt")  
  9.             While Not sR.EndOfStream  
  10.                 Dim word As String = sR.ReadLine  
  11.    
  12.                 wordList.Add(word)  
  13.    
  14.                 lbl.Invoke(Sub()  
  15.                       lbl.Text = word  
  16.                       counter.Text = wordList.Count.ToString  
  17.                       Me.Refresh()  
  18.                     End Sub)  
  19.             End While  
  20.         End Using  
  21.    
  22.         lbl.ForeColor = Color.Green  
  23.     End Sub  
  24.    
  25.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
  26.         Me.DoubleBuffered = True  
  27.    
  28.         Dim _x As Integer = 8  
  29.         Dim _y As Integer = 40  
  30.    
  31.         For ii As Integer = Asc("a"To Asc("z")  
  32.    
  33.             Dim c As New Label  
  34.             c.Name = "Label" & ii.ToString("000")  
  35.             c.Text = "---"  
  36.             c.Top = _y  
  37.             c.Left = _x  
  38.             Me.Controls.Add(c)  
  39.    
  40.             _y += 20  
  41.             If _y > 180 Then  
  42.                 _y = 40  
  43.                 _x += 120  
  44.             End If  
  45.    
  46.             Dim j As Integer = ii  
  47.             Dim t As New Task(Sub()  
  48.                   AddWords(Chr(j), CType(Me.Controls("Label" & j.ToString("000")), Label))  
  49.                  End Sub)  
  50.    
  51.             t.Start()  
  52.         Next  
  53.    
  54.     End Sub  
  55.    
  56. End Class 
The project is a simple WinForms program. During Load() event, we'll create all the Labels we need, starting the tasks that will use, each of them on a different word, the AddWords() subroutine, to process a single dictionary (dictionary files will be found in the downloadable project, at the end of the article). We will see, taking a look at the loop in the Load() event, that each created task is launched immediately, letting the OS the worries of queueing those processes which aren't immediately manageable.

Each task, calling the AddWords() subroutine, will provide in: 1) opening the file having a given letter, 2) read each line, 3) save that value in the List wordList. We will also note a calling to the method Invoke() on the Label passed as argument to the subroutine. An interesting particular is that inside a single Invoke() a developer can manage more than a single UI update. In our specific case, we can see how the Label is updated, updating also the one named "counter", which we will use to show the global number of words read to a given moment. Moreover, to help the graphical rendering of our controls, we will call upon the Refresh() method of the Form itself, though - doing so - we will impair the overall performances, since the program will dedicate part of its cycles to refresh the Form and all its children, at every iteration.

As we can see running the code, or through the following video, the update of the content of our controls will happen in a concerted way, allowing each task to modify the value of controls which constitutes the UI of our program.

Demonstrative video

A demonstrative video of the running program is available.

Download

The source code of the example above, and its auxilliary files, can be freely downloaded from the following URL.


Similar Articles