Page 1 of 1

Making a cross threaded UI (WinForms). Rate Topic: -----

#1 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Posted 29 May 2011 - 07:16 AM

Hello everyone! Today, I will show you how to make a multi-threaded UI application. For those of you who do not know what a thread is, I will give my simplest definition. A Thread is having multiple arms. You can do one task with one arm and do another with the other and so on. If you want a more detail definition, see the wikipedia article here.

Getting Started

Okay now that we cleared that up, it is now time to start coding the UI. Open VS (or VB Express) and create a VB Windows Forms (sorry, I'll make another tutorial for WPF). Name it "Counter". This is a simple form which will count to 30 on a thread and print it to a TextBox.

Drag two Buttons and a Multi-line TextBox to your form. To get a multi-line TextBox, you must set its multi-line property to True. Make sure the TextBox fills up the bottom half of the window. Name the TextBox as "CounterBox" and rename the Button1 as "CountBtn". Rename the second button as "CountAsyncBtn". Don't forget to make CountBtn's Text value as "Count" and other button as "Count Async" or "Count Threaded" (your choice).

Double click the "Count" button. You are now in the Button's Click_Event. Add the following code to its Click_Event:
'The following is an example of counting without a thread.
For i as Integer = 0 to 100
   Application.DoEvents() 'This simulates a delay but it really keeps the UI from freeze so you can see the numbers go up.
   CounterBox.AppendText(i.ToString() + vbNewLine)
Next



You make get some compiler errors at this point but don't worry. To fix them, add the following to the top of the file:
Imports System.Windows.Forms 'Only add this if its not already there
Imports System.Threading 'Add this for later



If you like, you can stop and run the count at this point and see how it counts. Once thats finished, we can continue with the threaded example.

Double click the CountAsyncBtn and add the following code:
'Normally, threads require you to pass code in a separate function but I am using a new technique called anonymous methods.
Dim thr As New Thread(Sub()
                        For i as Integer = 0 to 100
                           Thread.Sleep(100) 'This adds a simple delay so you can see it count and also to keep it from eating up memory.
                           Me.Invoke(Sub() 
                                        'Same with the threads, it requires a function delegate but instead, I will pass a anonymous method.
                                        CounterBox.AppendText(i.ToString() + vbNewLine)
                                     End Sub)
                        Next
                      End Sub)
thr.IsBackground = True
thr.Start() 'Starts the thread



With all of that code written, your form's code should look something like this:
Imports System.Windows.Forms 'Only add this if its not already there
Imports System.Threading 'Add this for later
Public Class Form1

    Private Sub CountBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountBtn.Click
        For i As Integer = 0 To 100
            Application.DoEvents() 'This simulates a delay but it really keeps the UI from freeze so you can see the numbers go up.
            CounterBox.AppendText(i.ToString() + vbNewLine)
        Next
    End Sub

    Private Sub CountAsyncBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountAsyncBtn.Click
        'Normally, threads require you to pass code in a separate function but I am using a new technique called anonymous methods.
        Dim thr As New Thread(Sub()
                                  For i As Integer = 0 To 100
                                      Thread.Sleep(100) 'This adds a simple delay so you can see it count and also to keep it from eating up memory.
                                      Me.Invoke(Sub()
                                                    'Same with the threads, it requires a function delegate but instead, I will pass a anonymous method.
                                                    CounterBox.AppendText(i.ToString() + vbNewLine)
                                                End Sub)
                                  Next
                              End Sub)
        thr.IsBackground = True
        thr.Start() 'Starts the thread
    End Sub
End Class



You should see it counting from a thread.

Downloads
You can download my sample project from here. It requires Visual Studio 2010 (or VB Express 2010).Attached File  Counter.zip (11.45K)
Number of downloads: 317

Good luck with your many programming adventures.

Is This A Good Question/Topic? 2
  • +

Page 1 of 1