Solved

Events in Control Arrays

Posted on 2002-03-12
4
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 - Convert HH:MM into Decimal 8 70
Problem to open text file 11 136
Math Equation 23 122
Remove Exif Orientation before adding image to db 9 38
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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