Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Events in Control Arrays

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

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

886 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