ADO DOT NET Posted February 6, 2007 Posted February 6, 2007 (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 February 6, 2007 by ADO DOT NET Quote
Administrators PlausiblyDamp Posted February 6, 2007 Administrators Posted February 6, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 6, 2007 Author Posted February 6, 2007 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? Quote
Administrators PlausiblyDamp Posted February 6, 2007 Administrators Posted February 6, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 7, 2007 Author Posted February 7, 2007 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... Quote
Leaders snarfblam Posted February 8, 2007 Leaders Posted February 8, 2007 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.