Threading issue with VB6

   I am developing a winsock server using VB 6. I have created My winsock OCX using api programming in VB 6. Now that I have the OCX, I tried to load an array of my ocx in a vb exe project ( My Server Application , Chat server ).

For each connection, I would load one of my control using the index property of the ocx control.

I am using      load srv(1)   ' to load controls
and                unload srv(1) ' to unload controls

The problem is that all the controls I load, work on the same thread as the parent application.

I need all my controls ( the whole array ) to share a thread pool. How can I do it.

I though of Activex EXE. So the problem is that I am not able to load an array of activex exe dlls in my exe project and use different events associated with the control. As My parent application needs to know certain issues like activity status, connected number , disconnected number, i need to use events. We are able to set the index property of a normal ocx in our project and load an array of that control, So then why cant we load an activex exe dll array.

Can some one please suggest me a solution for my problem

Thanks in advance
Noby E A
Who is Participating?
StimphyConnect With a Mentor Commented:
Create an ActiveX EXE project for all your sockets.  This runs outside of the application thread.

I develope something just the way you wanted, i did it by creating a form with a winsock object,  and in the program i create a dinamyc array of this form.

Every time a connection was accepted i check for closed connections (a public property of the form) or create a new one to be waiting for the next connection.

Each instanced form will work alone in a new thread...

Thats the way i solve the problem...

I Hope it could be useful for you

By the way, you can create ActiveX objects with visual components you just have to look how to do it
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

xlteknologiesAuthor Commented:
Your suggestion is very much valid. But loading so many forms, is it okay.

For eg, if the server should give approximately 5000 connections, 5000 forms will be loaded. I am not sure whether this will create any issue. Are you sure this will work.

Another issue I faced is, even if we keep the form property as invisible and load the form in a different thread, it displays. Then I inserted the code in the form load to make it invisible. still it flickers on the screen. can this be solved.


In the ocx program, you have to instantiate the ocxs like what we do with forms like this,

Dim a as new frmInstant

So, when ever the ocx is instantiated in your vb proj like ocxcontrol.load(1), then it will call new instance not the existing one.

Sorry, am running out of time so i cant breif the answer more. But, ask me if you are not able to understand the thing what iam speaking.

If you got some hint thru my answer, let me to bag the points :)

xlteknologiesAuthor Commented:
   I did not really understand what you explained. if I am correct, in the initialise part of the user control on the OCX, we have to write some code to open the usercontrol in a new thread.

I would be very gratefull if you can provide me some light into this matter, on how to do it.

Bye !!
OK... to clarify alittle bit,
   You may not need an array of classes or winsocks available to your application, after all, your only interested in getting data that is sent to the hosting application right?

So in the activeX EXE, you could do something like this..
'Add a form and put your sockets on it, with your code
'Add this code to your public class and intergrade it into the form code.
'Build It

'This event will be used to tell you that some data has arrived from this socket.
'Your application receives the calling socket, so if a response is needed, you can do it.
Public Event DataArrival(tempSocket As Winsock, Data As String)

'Maybe you want to block certin host connections... Do it with this.
Public Event ConnectionRequest(RemoteHost As String)
Public BlockConnection as boolean

Friend Sub DataEvent(tempSocket As Winsock, Data As String)
    RaiseEvent DataArrival(tempSocket, Data)
End Function

Friend Sub ConnectionEvent(RemoteHost As String) as boolean
    BlockConnection=False  'Available for the hosting appliction
    RaiseEvent ConnectionRequest(RemoteHost)
    ConnectionEvent = BlockConnection
End Function

Obviously there is more work to do, but this should get you on the right track.

Julian_CConnect With a Mentor Commented:
If you just need to slip the current thread and call methods on another then there are countless examples using a call to the WIn32 timer API, passing the addressof the method you wish to call. In this way the method is called when the timer fires in a new thread that the timer creates. You just set the timer to fire almost instantly and there is no discernable performance hit. If this is the direction you want to go then let me know and I'll dig out some examples. The concept sounds easy enough and I've used it myself to great effect but it is very hard to debug etc as the multithreading is not understood natively. I always debug single threaded once I've proved that the multihtreading works and just turn on the multithreading in the compiled version.

Do you need an example or is this way off the mark?

Personally, I think that the answers were on the wrong lines up to Stimpy's comment. I added mine as a sort of lateral jump into multithreading in VB6. Other comments really used multithreading by using different processes for each thread. My solution provides multithreads in the same process space. It isn't offically supported but it works and came in very handy for me in the bad old days before .net :-) Anyway, I think the points should be split down the middle. OK?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.