Adagio
Well-known member
- Joined
- Dec 12, 2005
- Messages
- 162
- Programming Experience
- Beginner
What would be the best way to solve this problem:
I have a class (let's call it TestClass) and a form. When I create an instance of TestClass it starts a thread, which runs constantly in the background. Now and then the background-thread raises an event, which the form listens for. When the event is raised, the form should update some controls on the form
Anyone who has ever worked with threads, know what probably would happen: Cross-thread operation error
Here is the code I have:
Now before anyone comes with the normal responses, that you normally read as answers in these questions, I would like to say that I know at least one way to solve this problem:
In the CountChanged sub on the form it can be changed to this:
It's a solution, but IMHO not a good solution, I'm looking for a more userfriendly solution
It would be better if you didn't have to do this on the form, so that those who uses my class shouldn't have to worry about Threads. What would be the best way to solve this problem?
I have a class (let's call it TestClass) and a form. When I create an instance of TestClass it starts a thread, which runs constantly in the background. Now and then the background-thread raises an event, which the form listens for. When the event is raised, the form should update some controls on the form
Anyone who has ever worked with threads, know what probably would happen: Cross-thread operation error
Here is the code I have:
VB.NET:
Public Class Form1
Private WithEvents c As New TestClass
Private Delegate Sub UpdateUIDelegate(ByVal index As Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'AddHandler c.CountChanged, AddressOf CountChanged
c.Count()
End Sub
Private Sub CountChanged(ByVal index As Integer) Handles c.CountChanged
ProgressBar1.Value = index
End Sub
End Class
Public Class TestClass
Public Sub Count()
Dim t As New Threading.Thread(AddressOf CountingTest)
t.Start()
End Sub
Private Sub CountingTest()
Dim index As Integer = 0
While True
index += 1
RaiseEvent CountChanged(index)
Threading.Thread.Sleep(1000)
End While
End Sub
Public Event CountChanged(ByVal index As Integer)
End Class
Now before anyone comes with the normal responses, that you normally read as answers in these questions, I would like to say that I know at least one way to solve this problem:
In the CountChanged sub on the form it can be changed to this:
VB.NET:
Private Delegate Sub UpdateUIDelegate(ByVal index As Integer)
If Me.InvokeRequired Then
Dim Parms() As Object = {index}
Me.Invoke(New UpdateUIDelegate(AddressOf CountChanged), Parms)
Else
ProgressBar1.Value = index
End If
It's a solution, but IMHO not a good solution, I'm looking for a more userfriendly solution
It would be better if you didn't have to do this on the form, so that those who uses my class shouldn't have to worry about Threads. What would be the best way to solve this problem?