473,386 Members | 1,830 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.

Question about threads

I am looking at the Creating a Multi-User TCP Chat Application
and I do not understand something. They say that we cannot
directly add the received text to the textbox of the client because
the receiving thread from the network stream is not the main thread.
The solution offered is to use the me.invoke method to switch to
the main thread, i.e.

Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)

I can't see how this switches back to the main thread, Me refers to
the current instance, which is not the main thread,
AddressOf Me.DisplayText is the address of DisplayText in the current
thread ?
Nov 21 '05 #1
4 999
When Invoke (or BeginInvoke) is called, the runtime takes care of crossing
the thread boundary and marshalling data across from one thread to the main
UI thread. Here's some more info on this:
http://weblogs.asp.net/justin_rogers...es/126345.aspx

hope that helps..
Imran.

"C.A." <nospam> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
I am looking at the Creating a Multi-User TCP Chat Application
and I do not understand something. They say that we cannot
directly add the received text to the textbox of the client because
the receiving thread from the network stream is not the main thread.
The solution offered is to use the me.invoke method to switch to
the main thread, i.e.

Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)

I can't see how this switches back to the main thread, Me refers to
the current instance, which is not the main thread,
AddressOf Me.DisplayText is the address of DisplayText in the current
thread ?

Nov 21 '05 #2
First, define an instance variable for your main form in your thread class:

Private m_MainForm as MyMainFormClass

When you create your thread, pass in the main form instance and store in
m_MainForm. Now your thread has an instance to communicate with.

Next, define a delegate in your thread class:

Private Delegate Sub _SendString_Delegate(ByVal theString As String)

Also, create a method in your thread class for sending a string to the main
thread:

Public Sub SendString(ByVal theLabel As String)

Dim Parameters(0) As Object

Parameters(0) = theString

Try

m_MainForm.Invoke(New _SendString_Delegate(AddressOf
m_MainForm.DisplayText), Parameters)

Catch ex As Exception

End Try

End Sub
Execute this method in your thread whenever you wish to send a string

Finally, in your main form, add a method "DisplayText(byval theString as
String)".
"C.A." <nospam> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
I am looking at the Creating a Multi-User TCP Chat Application
and I do not understand something. They say that we cannot
directly add the received text to the textbox of the client because
the receiving thread from the network stream is not the main thread.
The solution offered is to use the me.invoke method to switch to
the main thread, i.e.

Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)

I can't see how this switches back to the main thread, Me refers to
the current instance, which is not the main thread,
AddressOf Me.DisplayText is the address of DisplayText in the current
thread ?

Nov 21 '05 #3
So Me will refer to the form thread, so that's OK.

Are there any other ways to do this, i.e. if I have a second thread
and I want to update information in the form, this is not thread safe,
so I can use me.invoke, Can I use SyncLock, or events?
Would this work:
assume textbox1.text is on a form, and we are on a different thread,
can we do this safely:

SyncLock
textbox1.text="1234"
end SyncLock

If so, why bother with delegate/invoke, this is much simpler to code?
Nov 21 '05 #4
The way I understand it, you might get deadlocked on your main thread if you
do that. Invoking the delegate should ensure this doesn't happen.

I'm not 100% sure either whether invoking the delegate from multiple threads
is safe. I assume the invocations are queued somewhere and executed on your
main thread one at a time. To be safe you could synchronise in your main
thread, but someone with more knowledge (my program only uses a single
worker thread) about threading might be able to help and possibly avoid the
synchronisation overhead.
"C.A." <nospam> wrote in message
news:Oz**************@tk2msftngp13.phx.gbl...
So Me will refer to the form thread, so that's OK.

Are there any other ways to do this, i.e. if I have a second thread
and I want to update information in the form, this is not thread safe,
so I can use me.invoke, Can I use SyncLock, or events?
Would this work:
assume textbox1.text is on a form, and we are on a different thread,
can we do this safely:

SyncLock
textbox1.text="1234"
end SyncLock

If so, why bother with delegate/invoke, this is much simpler to code?

Nov 21 '05 #5

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

Similar topics

1
by: Randy Geyer | last post by:
I have an NT/W2K service that spawns 3 worker threads that each do different things. For example, one monitors an email mailbox for incoming items and puts them in a queue for processing by a 2nd...
2
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
3
by: jackowilkinson | last post by:
assuming code: int i=0;//global variable //process 1 //process 2 int num=0; int num=0; while(i<5){ while(i<5){ num=num+1; num=num+1; i=i+1; i=i+1;...
2
by: Leon Mergen | last post by:
Hello, Ok, I'm having a problem when trying to make the classes I make a bit more generic. Consider this: I have one class, ClassA. This defines some interface methods, and ClassB inherits...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
5
by: Peter Kirk | last post by:
Hi, I see in the ThreadPool documentation that the pool has a default limit of 25 threads. Is it correctly understood that this limit is for my entire application? So if I have several...
6
by: Padhu Vinirs | last post by:
JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would like to print some status of the child threads from the main thread periodically. But I dont see the main thread to print...
27
by: Ritesh Raj Sarraf | last post by:
Hi, I have some basic doubts about thread. I have a list which has items in it which need to be downloaded from the internet. Let's say list is: list_items which has 100 items in it.
3
by: Chris Roth | last post by:
I'm using VS.net 7.1 on Windows XP. I have a class that hold a container of doubles. One function (foo) for the class calls a sub-function (bar) for each of the doubles. I'd like to...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.