Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Events in Control Arrays

Posted on 2002-03-12
4
Medium Priority
?
178 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 53

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 200 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

715 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