tommykent1210
Member
- Joined
- Nov 2, 2011
- Messages
- 10
- Programming Experience
- 1-3
Hey guys (and girls?),
I've been programming in vb.NET for a while, and I've just began to sink my teeth into threading. And to be honest, its turning out to be a real pain.
Here is the code:
Form1.vb:
Class1.vb:
So, I have a form (form1) with a label on, this label simply reports the status of the thread. The thread begins the Class1.Main() sub, and I have a delegate setup to change the text propoerty of the label.
Now, I know the label is created on the UI thread, so I assumed the delegate would work. However, it seems that it updates the .text property, but doesnt redraw the control when .Update() is called
What am I doing wrong?
I have found lots of examples of delegates, but all run from within the same class. I want to use threads too, not a backgroundworker! This code is a "simplified" version of a project I am on, which is encountering the same problems
Thanks,
Tom
I've been programming in vb.NET for a while, and I've just began to sink my teeth into threading. And to be honest, its turning out to be a real pain.
Here is the code:
Form1.vb:
Imports System.Threading Public Class Form1 Dim trd As Thread Public Shared classy As New Class1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load trd = New Thread(AddressOf Dostuff) trd.IsBackground = True trd.Start() End Sub Public Delegate Sub SetTextDelegate(ByVal TheText As String) Private Sub delSetText(ByVal TheText As String) Label1.Text = TheText Label1.Update() End Sub Public Sub ChangeText(ByVal TheText As String) If Label1.InvokeRequired Then BeginInvoke(New SetTextDelegate(AddressOf delSetText), TheText) Else delSetText(TheText) End If End Sub Sub Dostuff() classy.Main() End Sub End Class
Class1.vb:
Imports System.Threading Public Class Class1 Public Sub Main() For i As Integer = 0 To 100 Form1.ChangeText("button " & i) Next End Sub End Class
So, I have a form (form1) with a label on, this label simply reports the status of the thread. The thread begins the Class1.Main() sub, and I have a delegate setup to change the text propoerty of the label.
Now, I know the label is created on the UI thread, so I assumed the delegate would work. However, it seems that it updates the .text property, but doesnt redraw the control when .Update() is called
What am I doing wrong?
I have found lots of examples of delegates, but all run from within the same class. I want to use threads too, not a backgroundworker! This code is a "simplified" version of a project I am on, which is encountering the same problems
Thanks,
Tom