Solved

Events in Control Arrays

Posted on 2002-03-12
4
159 Views
Last Modified: 2013-11-25
I have a control array of the winsock control (sckTCP). Each control receives data from a computer on the network. The sckTCP_DataArrival event calls another routine (SaveDataToDB) that saves the received data to a database. What happens if at least two controls in the array receive data and call the SaveDataToDB routine simultaniously? Also can a single control receive more data while the SaveDataToDB routine has not returned control to its sckTCP_DataArrival event?
0
Comment
Question by:bmatumbura
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6856193
Basically, Each Control in Controls Array got a Index Property, use this Index will differentiate each of them, isn't it?
0
 
LVL 39

Accepted Solution

by:
abel earned 50 total points
ID: 6856444
The events are queued in a certain way, following the rules of apartment threading. Free threading, which allows semi-simultaneous actions, is not supported by VB. But don't worry, unless you really block your events, you should not have to bother about this: for you and your users it appears as if data comes in simultaneously. And still, while coding, you can differentiate like ryancys says: using the Index parameter in the event.

Cheers,
Abel
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6856568
I don't think you have to worry. Each time the Event is called a new dataspace is created for the event.  Although the variables are the same name the data will be different.

Don't store the event details on global variables or use staic variables.  If you post the code we can tell you if it looks ok.

Further point; VB does not multi-task so in real life events are queued. But you yield your event with a "Sleep" or "DoEvents" the same event can ne called at the same time (but with differnt values in the variables).

And another point if you don't like using Indexes you can place your Winsock control in a Class module and use the

Public WithEvents MySock as Winsck?

In the Class Initialze event MySock is instantiated like:

Set MySock = frmDummy.AWinsockControl

(You can find an example of this at www.freevbcode.com search for vbSendMail.)

In your class project you can create a dummy form with a Winsock control on it.

The advantage of this method is that you can make each Winsock control as a sepeterate EXE and build a truely multi-tasking app wich could handle a much higher level of traffic.  You can communicate between the exe tasks by using your class.  You can run the tasks hidden so they don;t show on the task bar.

So in your code you could say thinks like:

Dim MyClass as new clsSocker

MyClass.SendMessage "YourServer", Port, DataToSend

do until MyClass.HasDataComeBack
   Doevents (or Sleep 100)
Loop

debug.print MyClass.DataReceived

All the events are handled inside MyClass.

SO you can create an attay of them by using a collection:

Dim Socks as Collection

Dim MyClass as clsSocker

for c= 1 to 10
  Set MyClass = CreateObject("MyProject.MyClass)
  Socks.Add MyClass, cstr(C)
Next c

' boardcast messages
for c= 1 to 10
  Socks(c).SendMessage ServerList(c),PortList(c),BroadCastText
Next c

Hope this helps.
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6857760
Sorry my example was very simple but the class approach makes server management simple.

For each new connection request you simple create a new instance of itself and add it to a collection.


e.g.

In your class:

Dim Clients as collection

Private Sub Sock_ConnectionRequest(ByVal requestID As Long)

Dim OutsideWorld As MyClass
Set OutsideWorld = New MyClass

OutsideWorld.Sock.Tag = requestID
Clients.Add OutsideWorld, requestID

End Sub

In you form/module you can use syntax like


Dim Server as MyClass
Set Server = New Myclass

Server.Sock.LocalPort = 1234
Server.Sock.Listen

' and later you can say:

dim Client as MyClass
For Each Client in Server.Clients
   Client.SendData "?????"
End IF


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now