AlwayzLearning
New member
- Joined
- Nov 29, 2014
- Messages
- 1
- Programming Experience
- 1-3
I am new to threading, and am struggling with some simple code that displays the affects of threading as an example. The code is not originally mine and I don't know who the credit goes to for making it. The example I have allows for a separate thread to update the UI, but when i go to execute a different thread while the first one is running it stops updating the UI until they are both done executing. I would like to be able to see both threads update the UI simultaneously without hanging up the updates.
After reading several forums and such I think I should be looking into Asynchronous threading. Is there anyway somebody here can shed some light on this for me and possibly point out corrections that would need done to make this work?
Any help would be greatly appreciated! Thank you in advance!
(Also I don't know how to post blocks of code like the above examples... Code is pasted below)
After reading several forums and such I think I should be looking into Asynchronous threading. Is there anyway somebody here can shed some light on this for me and possibly point out corrections that would need done to make this work?
Any help would be greatly appreciated! Thank you in advance!
(Also I don't know how to post blocks of code like the above examples... Code is pasted below)
Imports System.Threading Public Class Form1 Delegate Sub callback1(ByVal [text] As String) Delegate Sub callback2(ByVal [text] As String) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim count As New Thread(New ThreadStart(AddressOf count1)) count.Start() End Sub Private Sub count1() Dim rna As New Random Dim i As Integer i = 0 Do Until i = 1000 i = i + 1 setText1(rna.Next(1, 1000)) Loop End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim count As New Thread(New ThreadStart(AddressOf count2)) count.Start() End Sub Private Sub count2() Dim rna As New Random Dim i2 As Integer i2 = 0 Do Until i2 = 1000 i2 = i2 + 1 setText2(rna.Next(1, 1000)) Loop End Sub Private Sub setText1(ByVal [text] As String) If Me.TextBox1.InvokeRequired = True Then Dim d As New callback1(AddressOf setText1) Me.Invoke(d, New Object() {[text]}) Else TextBox1.Text = [text] Label1.Text = [text] End If End Sub Private Sub setText2(ByVal [text] As String) If Me.TextBox2.InvokeRequired = True Then Dim d As New callback2(AddressOf setText2) Me.Invoke(d, New Object() {[text]}) Else TextBox2.Text = [text] Label2.Text = [text] End If End Sub End Class
Last edited by a moderator: