Threading Problem

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hello everyone:)
I found this great sample from MSDN:
Visual Basic:
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
Public Class Form1
    Delegate Sub SetTextCallback(ByVal [text] As String)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
        thRead.Start()
    End Sub
    Private Sub myThread(ByVal o As Object)
        Me.SetText("a")
    End Sub
    Private Sub SetText(ByVal [text] As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.TextBox1.Text = [text]
        End If
    End Sub
End Class
This clearly shows how to set the text of a text box on a thread.
I am new to threading and have some problems with that.
For example, I do not need to set the text inside the text box, but I need to disable the text box.
So, something is still complicated for me:
How should I change the following lines to meet my need?
---
Delegate Sub SetTextCallback(ByVal [text] As String)
---
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
---
Please help me to learn this important issue of programming:)
 
Last edited:
Not tested (or even compiled) but something like
Visual Basic:
Public Class Form1

    Delegate Sub SetEnabledCallback(ByVal enabled As Boolean)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
        thRead.Start()
    End Sub

    Private Sub myThread(ByVal o As Object)
        Me.SetText("a")
    End Sub

    Private Sub SetEnabled(ByVal enabled As Boolean)
        If Me.TextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetEnabled)
            Me.Invoke(d, New Object() {enabled})
        Else
            Me.TextBox1.Enabled = enabled
        End If
    End Sub
End Class

should do the trick.
 
Many thanks PlausiblyDamp,
You helped me a lot, god bless you! :)
I just have another question related to my code, it always set something on form, I need to do something that might be so complicated for me.
I am running this thread code on a 2nd form which has been called from 1st form, I need to read the text of a combo box in form1 from form2 in my thread safely.
Could you be so kind and advise me how should I do this?
 
The basics of forms passing information around can be found in this thread

Threads are only a problem when dealing with UI elements from a background thread - the general idea of how to do this is exactly the same as in your above post.
 
Thank you:)
Yes but I can easily get the text of combo box in form1 from form2 with this:
MsgBox Form1.ComboBox1.Text
Just in a thread can't get it...
 
If I understand your question correctly, then the answer should be that to perform an action in a thread-safe manner, the action can be wrapped in a public method and invoked. More specifically, you can define a public function in Form1 that will return the value of the combo box, and then use Form1's invoke method to invoke that function in a thread-safe manner.
 
Back
Top