473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

A thread and textbox question

I have a form with a text box, a thread start button, and a seperate thread.
This is a TCP listener (server) example I took out of a book. See the code
below. In the loop below, look at the two lines I have marked with a comment
of ??????? If I don't write to the text box, I have a fast response. The
value "returnedData" is written to the console in a timely manner. However,
if I try to write to the textbox, tbStatus, the response slows to about 1
time per second. Is there some problem with writing to a textbox from within
a seperate thread?? Can somesone suggest a solution.

Hamil.

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
btnStop.Enabled = True
Dim tr As New Thread(AddressOf ListenToClients)
tr.Start()
End Sub

Sub ListenToClients()
'localhostAddress = IPAddress.Loopback
localhostAddress = IPAddress.Parse("192.168.1.5")
port = CInt(txtPort.Text)
tcpList = New TcpListener(localhostAddress, port)
tcpList.Start()
listening = True
Do While listening
Do While tcpList.Pending = False And listening = True
Thread.Sleep(10)
Loop
If Not listening Then Exit Do
Dim tcpCli As New TcpClient
tcpCli = tcpList.AcceptTcpClient()
ns = tcpCli.GetStream
sr = New StreamReader(ns)
sw = New StreamWriter(ns)
tbStatus.Text = "connected"
Do While listening = True
Thread.Sleep(10)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = ProcessInput(receivedData)

Console.WriteLine(returnedData) '???????????
tbStatus.Text = returnedData '???????????????

sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
tbStatus.Text = "closed"
Loop
tcpList.Stop()
End Sub
Nov 21 '05 #1
2 6210
Technically, you shouldn't be accessing a control from any other thread
except the one it was created on.
http://msdn.microsoft.com/library/de...voketopic1.asp

Accessing controls from a secondary thread is not guaranteed. It might work
sometimes but you shouldn't rely on it.

Instead, you should define a delegate and use the delegate to marshal the
calls from the secondary thread to the main thread when accessing your
controls. I'm not sure if this would speed up the update but here's how you
would update a textbox's text from another thread:
Delegate Sub SetTextDelegate(ByVal sText As String)
Private SetText As New SetTextDelegate(AddressOf SetTextBoxText)
Private Sub SetTextBoxText(ByVal sText As String)
tbStatus.Text = sText
End Sub

Sub ListToClients()
...
' all your relevant code..
...
...
...
' replace tbStatus.Text = "Connected" with this..
tbStatus.Invoke(SetText, New Object() {"Connected"})
...
' all your remaining code..
...
...
End Sub
hope that helps..
Imran.

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
I have a form with a text box, a thread start button, and a seperate thread. This is a TCP listener (server) example I took out of a book. See the code
below. In the loop below, look at the two lines I have marked with a comment of ??????? If I don't write to the text box, I have a fast response. The
value "returnedData" is written to the console in a timely manner. However, if I try to write to the textbox, tbStatus, the response slows to about 1
time per second. Is there some problem with writing to a textbox from within a seperate thread?? Can somesone suggest a solution.

Hamil.

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
btnStop.Enabled = True
Dim tr As New Thread(AddressOf ListenToClients)
tr.Start()
End Sub

Sub ListenToClients()
'localhostAddress = IPAddress.Loopback
localhostAddress = IPAddress.Parse("192.168.1.5")
port = CInt(txtPort.Text)
tcpList = New TcpListener(localhostAddress, port)
tcpList.Start()
listening = True
Do While listening
Do While tcpList.Pending = False And listening = True
Thread.Sleep(10)
Loop
If Not listening Then Exit Do
Dim tcpCli As New TcpClient
tcpCli = tcpList.AcceptTcpClient()
ns = tcpCli.GetStream
sr = New StreamReader(ns)
sw = New StreamWriter(ns)
tbStatus.Text = "connected"
Do While listening = True
Thread.Sleep(10)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = ProcessInput(receivedData)

Console.WriteLine(returnedData) '???????????
tbStatus.Text = returnedData '???????????????

sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
tbStatus.Text = "closed"
Loop
tcpList.Stop()
End Sub

Nov 21 '05 #2
What you say works! I tried it. Thanks.

Now, if the text box I want to write into is in another form, how would I
handle this?

I would assume I would do something like..

dim RemoteTextBox as textbox

Private Sub SetTextBoxText(ByVal sText As String)
RemoteTextBox.Text = sText
End Sub

I would have to set the RemotTextBox object variable from the remote form.
Comments??

hamil.


"Imran Koradia" wrote:
Technically, you shouldn't be accessing a control from any other thread
except the one it was created on.
http://msdn.microsoft.com/library/de...voketopic1.asp

Accessing controls from a secondary thread is not guaranteed. It might work
sometimes but you shouldn't rely on it.

Instead, you should define a delegate and use the delegate to marshal the
calls from the secondary thread to the main thread when accessing your
controls. I'm not sure if this would speed up the update but here's how you
would update a textbox's text from another thread:
Delegate Sub SetTextDelegate(ByVal sText As String)
Private SetText As New SetTextDelegate(AddressOf SetTextBoxText)
Private Sub SetTextBoxText(ByVal sText As String)
tbStatus.Text = sText
End Sub

Sub ListToClients()
...
' all your relevant code..
...
...
...
' replace tbStatus.Text = "Connected" with this..
tbStatus.Invoke(SetText, New Object() {"Connected"})
...
' all your remaining code..
...
...
End Sub
hope that helps..
Imran.

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
I have a form with a text box, a thread start button, and a seperate

thread.
This is a TCP listener (server) example I took out of a book. See the code
below. In the loop below, look at the two lines I have marked with a

comment
of ??????? If I don't write to the text box, I have a fast response. The
value "returnedData" is written to the console in a timely manner.

However,
if I try to write to the textbox, tbStatus, the response slows to about 1
time per second. Is there some problem with writing to a textbox from

within
a seperate thread?? Can somesone suggest a solution.

Hamil.

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
btnStop.Enabled = True
Dim tr As New Thread(AddressOf ListenToClients)
tr.Start()
End Sub

Sub ListenToClients()
'localhostAddress = IPAddress.Loopback
localhostAddress = IPAddress.Parse("192.168.1.5")
port = CInt(txtPort.Text)
tcpList = New TcpListener(localhostAddress, port)
tcpList.Start()
listening = True
Do While listening
Do While tcpList.Pending = False And listening = True
Thread.Sleep(10)
Loop
If Not listening Then Exit Do
Dim tcpCli As New TcpClient
tcpCli = tcpList.AcceptTcpClient()
ns = tcpCli.GetStream
sr = New StreamReader(ns)
sw = New StreamWriter(ns)
tbStatus.Text = "connected"
Do While listening = True
Thread.Sleep(10)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = ProcessInput(receivedData)

Console.WriteLine(returnedData) '???????????
tbStatus.Text = returnedData '???????????????

sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
tbStatus.Text = "closed"
Loop
tcpList.Stop()
End Sub


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hector Martinez | last post by:
I need to know the position of the cursor inside the TextBox. How Can I do that Thanks in advantage...
1
by: melanieab | last post by:
Hi, If there's a textbox and the text entered is longer than what's visible (the textbox length), how do you make it so that the beginning chunk of text is visible (instead of the last part of...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
10
by: garyusenet | last post by:
I have a multiline textbox. The size of the text box should be 75 characters wide, and 5 lines in height like this: - <---75 characters--> <---75 characters--> <---75 characters--> <---75...
1
by: Bmack500 | last post by:
Hello! And thanks in advance. I have the following code which just basically creates a set of threads for me (Using svrList as my list of class instances to thread). Let's call the threads...
3
by: Jeff Williams | last post by:
Is there a way I can place a Mask on a TextBox input so that say only numbers from 0 to 9 can be entered. Regards Jeff
7
by: Localbar | last post by:
Hi all, In my form have more then 10 textbox. I would like to make all textbox when lostfocus backcolor is white, when gotfocus backcolor is yellow. But I don't want to write same code in the...
6
by: HockeyFan | last post by:
I'd like to have an AJAX textbox that after the user fills in, will auto-fill another textbox on the page without having a postback occur. It doesn't have to do any filtering or any other thing...
3
by: John Heitmuller. | last post by:
Hi, I just want to make sure I'm not missing something obvious. VB.Net controls like the CheckBox are implemented with a label attached to it. This is very handy. But the TextBox control has no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.