Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Go Back  Xtreme Visual Basic Talk > > > Sockets, Multithreading, and Listboxes... grr.


Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2011, 09:24 PM
Michieal Michieal is offline
Newcomer
 
Join Date: Sep 2011
Location: Altered State
Posts: 2
Default Sockets, Multithreading, and Listboxes... grr.


Hello,
I am having difficulty in dealing with listboxes and multithreading, while using the TCP Sockets...
the issue: my server program uses a list box to sort out and display connected users. The Sockets/TCP (et al) is set up on it's own thread. I keep getting "unsafe Threading" errors whenever I try to do anything with the listbox. The listbox is on the form (form1), while the second thread/tcp socket stuff is tucked away in a class (ConnectedClient, class1.vb).
I was able to get the textual communications (like sending text to and from the Textbox Controls on the form and the socket) using the microsoft example for text box access in a safe multi-threaded way. I don't fully understand the whole Delegate idea. I get that functions/subs are just addresses in memory, and that's great... (same as the old C void* usage)... but, how do I link it up to the listbox, to perform the actions I want with it, without the compiler telling me some obtuse and totally unhelpful message that is supposed to be what I need to fix it?
I'm getting frustrated with this, as I just want to be able to display a list of connected clients , and be able to use the list to perform operations on the associated client. I'd make it into a control, but I'm stilll really new to VB 2008 .Net (if this was VB6, no problem).
any help I can get would be really appreciated!
Thank you.
-M.
Reply With Quote
  #2  
Old 09-30-2011, 03:45 AM
DrPunk's Avatar
DrPunkSockets, Multithreading, and Listboxes... grr. DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I wrote some code for someone else that should help show what you need to do -> http://www.xtremevbtalk.com/showthre...85#post1386685

The main difference between that and yours is that you'll probably be passing a parameter to the method you're trying to call on the form's thread (i.e. the connected user).

So, your delegate has to match the method you're trying to call (i.e. with any parameters that the method has) and then you pass any parameters to BeginInvoke inside an array.

Code:
Dim args() As Object = {userNameVariable}
BeginInvoke(Marshaler, args)
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 09-30-2011, 07:41 AM
AtmaWeapon's Avatar
AtmaWeaponSockets, Multithreading, and Listboxes... grr. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Here's plenty of information on the topic. Any particular reason why the socket is on its own thread instead of using its asynchronous methods? For that matter, any particular reason why you're using the Socket class instead of the more elegant TcpClient/etc.? It's the difference between flying the space shuttle and driving a racecar.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 09-30-2011, 09:16 AM
Michieal Michieal is offline
Newcomer
 
Join Date: Sep 2011
Location: Altered State
Posts: 2
Default

okay. here's what I am using (kind of new at the sockets stuff, last time I used it, it was a winsock control): Private mClient As System.Net.Sockets.TcpClient (Imports System.Net.Sockets)
and the main sub that is hitting the issue is:
Private Sub UpdateClientList()
LbUsers.Items.Clear()
For Each cc As ConnectedClient In clients
LbUsers.Items.Insert(LbUsers.Items.Count, cc.Username)
Next

End Sub (in the Form, not the class)

LBUsers is a Listbox, ConenctedClient is the Class type, that sets up a client of mClients type. Clients is the collection of connections (which are of the ConnectedClient class type) and have an username, and two basic methods... Remove and GetClientByName...
the problem occurs when I call the update to populate the listbox, usually at the remove stage. (it works at the connect stage).

Being new to 2008, I got a "simple Server" and "simple Client" example, and built off of there, so... I am not overly sure why? it is on its own thread, other than to use DoEvents with it is hinky at best... and not doable at worst. (from the help section, it says that multi-threading is the preferred method, as it allows the socket communication to flow in the background, ensuring the communication works/lack of timeouts/etc.)

Thank you for your help, I am going to look at both posts...
-M.
Reply With Quote
Reply

Tags
listbox, multithreading, sockets


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr. Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
 
Sockets, Multithreading, and Listboxes... grr.
Sockets, Multithreading, and Listboxes... grr.
 
-->