473,412 Members | 1,921 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,412 software developers and data experts.

NetworkStream.BeginRead and Forms threading

Hi all,

I've got a basic TCP app that is giving me trouble. I have a separate
class that takes care of the TCP connection, and uses the NetworkStreams
BeginRead and EndRead with a callback function to deal with the server
response. All works well and the data is received OK. Once all the data
is received, the connection is closed and I fire an event indicating all
is finished. The event is handled in the main form of the application
and I can work with the returned data OK.

The problem arises when I try to add some of the data to a TreeView. It
complains that only the creating thread can modify the TreeViews data
and I should use BeginInvoke etc (all this so far I understand). So I
assume control is still with the thread created by BeginRead? Am I
correct? If so, is there any way of forcing control to return to the
main forms threads once the event is fired for the data being received?
I've been able to set values on several other controls, the TreeView is
the only one giving me problems (why is it so picky when none of the
other controls are).

TIA

Steve.
Nov 16 '05 #1
6 5725
Stephen Brooker <st****@gmail.com> wrote:
I've got a basic TCP app that is giving me trouble. I have a separate
class that takes care of the TCP connection, and uses the NetworkStreams
BeginRead and EndRead with a callback function to deal with the server
response. All works well and the data is received OK. Once all the data
is received, the connection is closed and I fire an event indicating all
is finished. The event is handled in the main form of the application
and I can work with the returned data OK.

The problem arises when I try to add some of the data to a TreeView. It
complains that only the creating thread can modify the TreeViews data
and I should use BeginInvoke etc (all this so far I understand). So I
assume control is still with the thread created by BeginRead? Am I
correct?
The thread the event handler gets called on will be whatever thread
raises the event - probably a threadpool thread, yes. (BeginRead
doesn't necessarily "create" a thread though, and there's no guarantee
that all the data will be read by one thread - usually you call
BeginRead multiple times until all the data has been read, and the
callbacks for those calls could each happen on a different thread.)
If so, is there any way of forcing control to return to the
main forms threads once the event is fired for the data being received?
By using BeginInvoke (or Invoke) exactly as it says you should.
I've been able to set values on several other controls, the TreeView is
the only one giving me problems (why is it so picky when none of the
other controls are).


You've been lucky, that's all. The golden rule of Windows Forms
threading is that you don't do *anything* with a control apart from
BeginInvoke, EndInvoke, Invoke, InvokeRequired, or CreateGraphics,
unless you're running on the thread which created the control.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
>
You've been lucky, that's all. The golden rule of Windows Forms
threading is that you don't do *anything* with a control apart from
BeginInvoke, EndInvoke, Invoke, InvokeRequired, or CreateGraphics,
unless you're running on the thread which created the control.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml


OK, thanks Jon.

So does that effectively mean that every function in the main form that
modifies the GUI, and gets called after the TCP connection is
established needs modification using Invoke etc?
Nov 16 '05 #3
Stephen Brooker <st****@gmail.com> wrote:
You've been lucky, that's all. The golden rule of Windows Forms
threading is that you don't do *anything* with a control apart from
BeginInvoke, EndInvoke, Invoke, InvokeRequired, or CreateGraphics,
unless you're running on the thread which created the control.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml


OK, thanks Jon.

So does that effectively mean that every function in the main form that
modifies the GUI, and gets called after the TCP connection is
established needs modification using Invoke etc?


If it's still in a thread other than the UI thread, yes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] wrote:
Stephen Brooker <st****@gmail.com> wrote:
You've been lucky, that's all. The golden rule of Windows Forms
threading is that you don't do *anything* with a control apart from
BeginInvoke, EndInvoke, Invoke, InvokeRequired, or CreateGraphics,
unless you're running on the thread which created the control.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml


OK, thanks Jon.

So does that effectively mean that every function in the main form that
modifies the GUI, and gets called after the TCP connection is
established needs modification using Invoke etc?

If it's still in a thread other than the UI thread, yes.


So is there no way to force control to return to the main thread and
stop running on the threadpool thread? I can use Invoke etc, but that
still returns control to the existing thread doesn't it (certainly seems
to with my code)?

Apologies for repeating my question but I'm trying to make sure I
understand this properly. This is the first time I've mucked about with
TCP (and indirectly threads) in C# and it seems to introduce an awful
lot of stuffing about simply to keep GUI responsiveness. 90% of my code
will execute after the TCP connection is established, so there is a lot
of extra code that will need to be changed/added. There's got to be an
easier way.

Thanks for your help by the way.
Nov 16 '05 #5
Stephen Brooker <st****@gmail.com> wrote:
If it's still in a thread other than the UI thread, yes.
So is there no way to force control to return to the main thread and
stop running on the threadpool thread? I can use Invoke etc, but that
still returns control to the existing thread doesn't it (certainly seems
to with my code)?


Just call BeginInvoke to another method and then return from the
current method. That will execute the other method back on the main UI
thread and then let the threadpool thread return to the pool.

You can't "shift" a stack from one thread to another though.
Apologies for repeating my question but I'm trying to make sure I
understand this properly. This is the first time I've mucked about with
TCP (and indirectly threads) in C# and it seems to introduce an awful
lot of stuffing about simply to keep GUI responsiveness. 90% of my code
will execute after the TCP connection is established, so there is a lot
of extra code that will need to be changed/added. There's got to be an
easier way.


Not really. It's a bit of a pain, but that's how you have to do it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6


Not really. It's a bit of a pain, but that's how you have to do it.


OK, thanks heaps for the help Jon. Very much appreciated.
Nov 16 '05 #7

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

Similar topics

4
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create...
2
by: DC | last post by:
Hi, I need to asynchronously read from a network (TCP) stream, and I am having trouble with retrieving whole blocks; I get a break in the data block every 1460 bytes which relates to network...
1
by: Manuel Costa | last post by:
Hi, i was testing a network application that i've been working on which use .net socket components. To read from a socket i use the networkstream beginread. If the other side dies, beginread...
2
by: psg | last post by:
Hello! I am developing Windows application which after sending a request to a server will listen to response, a response will be infinite in length, but will not be continuous in time. What I...
1
by: Nicholas Holder | last post by:
A client creates a connection to the server using the TCPListener/Client classes and transfers data via a NetworkStream. When the client connects, the server creates a process and redirects its...
3
by: Terry Olsen | last post by:
Dim clt as TcpClient Dim stm as NetworkStream clt = New TcpClient() clt.Connect("192.168.0.200", 9999) Do Until clt.Connected Loop stm = clt.GetStream ' --The following line throws "Object...
2
by: TDC | last post by:
I'm working on tying in a library that uses a TcpClient/NetworkStream/BeginRead. When that lib object is started, it connects and immediately does a BeginRead that stays pending until the socket...
1
by: Rogier | last post by:
Hello, I'm Writing my own NZb download App, using Async Network Streams en Callback delegates. The Problem is that if I Want tot Receive Large chunks of data (for instance the LIST NEWSGROUPS...
7
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the...
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:
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.