Solved

Events in Control Arrays

Posted on 2002-03-12
4
164 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 50

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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