Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hello everyone:)

I found this great sample from MSDN:

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:)

Edited by ADO DOT NET
  • Administrators
Posted

Not tested (or even compiled) but something like

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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?

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...