want to muti thread a Socket Prorgrame in VB

Posted on 2002-05-20
Last Modified: 2012-08-13
i want to do create a software which will listedn to a specific port and get date in xml from the client and processes it and returns back and xml stream.  I have used win sock ocx and created an array to accept multiple connections.  now i want to make it mulit threade it.  how can i do it in vb.  The entire stuff will be executed on a windows 2000 Server having SQL server 2000 as the back end database.  Alos note that that the processing of client request may take some time (approx 10-20 secs or more )
Question by:vinaikk

Accepted Solution

adg earned 50 total points
ID: 7022994
Multi-threading in VB6 is ugly.  I have done it quite sucessfully using ActiveX EXE's and can help you if you choose that approach. The other approach is to do direct calls to the thread API, which I do not recommend but some people swear by.  

Whichever way you choose, the hard part is not getting multiple threads - it is communicating between them without serializing. Again, I have done this in VB6 but it involved updating variables by pointers, which is generally not something people recommended in VB6.  

Expert Comment

ID: 7023613
i'm just learning and noticed that it handles multithreading real simply by using something called "delegates". if it is not too much trouble you should consider
LVL 26

Expert Comment

ID: 7023796
I agree partially with adg

>>Multi-threading in VB6 is ugly

It is true if you run single program

Another alternative is create a new activeEXE and wrap around the OCx call

And then call this activeEXE from your main program

you can create activeEXE as many as you want and because it is out-of process. It seems as running multi-threading VB
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 7092328
i guess you are right, ActiveX exe would be the best to use.  
Request you to help me in this (ActiveX EXE)
You can mail me at

will be waiting for your reply, it is quiet urgent

Vinai KK

Expert Comment

ID: 7093855
Hi, thanks for accepting my "answer".  Rather than working with you via email, I'll post to this thread and then I'll try to help you with the inevitable problems you'll have getting it to work. I'll post a multithread example (as a starting point) a little later today.

Expert Comment

ID: 7094027
Here is an example of multi-threading in VB6. The thread id is displayed to verify that it is multithreaded. It has to be an activex exe project.  It has to be complied. If you run it in the IDE, it should execute OK but it won't be multithreaded. See the comments below for the proper project settings - it won't work without them.  Also, communication between threads is automatically synchronized by VB (COM) so trying to pass information between threads is a bit of a problem. The application has to be architected very carefully or you'll lose the benefits of having multiple threads.

In this example, the main thread will create three worker threads and pass each of them the name of a different text file. Each of those threads will read their assigned file and report the number of records found. I added a project reference to Microsoft Scripting Runtime to use File System Objects to access the files.

A word about object reference management.  The main thread only uses a single object variable (Thread1) to create three threads and then sets it to nothing.  In other words it isn't retaining a reference to the worker threads. Also note that the ProcessFile proc uses a timer to kick off the long running proc and that the timer is running on the worker thread. Its purpose is to allow the call to return to the main thread right away - otherwise the main thread would stop and wait for the worker thread to complete.  

I hope this helps - let me know if you run into a snag and I'll try to assist.  
' MainModule.bas
Option Explicit
' ActiveX EXE Name = MTNF
' Startup Object = Sub Main
' Threading Model = Thread per Object
' Start Mode = Standalone
' !!! Must compile and run outside IDE !!!
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Long
Private frmMainThreadForm As MainThreadForm
Private Const PROC_CAPTION = "MTNF Main Thread Form"
Private Thread1 As ProcessFileObject

Sub Main()
   If 0 = FindWindow(vbNullString, PROC_CAPTION) Then
       'MsgBox "Main = " & Hex(App.ThreadID)
       Set frmMainThreadForm = New MainThreadForm ' create first form on pre-existing thread
       frmMainThreadForm.Caption = PROC_CAPTION
       Set frmMainThreadForm = Nothing 'don't need this reference, form loads itself and stays alive
until unloaded
   End If
End Sub

Public Sub CreateThreads()
  Set Thread1 = CreateObject("MTNF.ProcessFileObject")
  Thread1.ProcessFile "a.txt"
  Set Thread1 = Nothing
  Set Thread1 = CreateObject("MTNF.ProcessFileObject")
  Thread1.ProcessFile "b.txt"
  Set Thread1 = Nothing
  Set Thread1 = CreateObject("MTNF.ProcessFileObject")
  Thread1.ProcessFile "c.txt"
  Set Thread1 = Nothing
End Sub

Public Sub ReportRecordCount(FileName As String, RecCount As Long)
   MsgBox FileName & " contains " & CStr(RecCount) & " records. Reported by Thread " & Hex(App.ThreadID)
End Sub

' MainThreadForm.frm
Option Explicit
Private Sub Form_Initialize()
    Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
   Timer1.Enabled = False
   Unload Me
End Sub

' ProcessFileObject.cls
' Instancing=Multiuse <=====
Option Explicit
Private frmTimerForm As TimerForm
Private Sub Class_Initialize()
   'MsgBox "Worker = " & Hex(App.ThreadID)
End Sub

Public Sub ProcessFile(FileName As String)
   Set frmTimerForm = New TimerForm
   frmTimerForm.SetOwner Me
   frmTimerForm.StartTimer FileName
End Sub

Public Sub RecordCount(FileName As String)
   ' add project reference to Microsoft Scripting Runtime
   ' to use File System Objects
   Dim fso As New FileSystemObject
   Dim fil1 As File
   Dim ts As TextStream
   Set fil1 = fso.GetFile(FileName)
   Set ts = fil1.OpenAsTextStream(ForReading)
   ReportRecordCount FileName, ts.Line
   Set frmTimerForm = Nothing
End Sub
' TimerForm.frm
Option Explicit
Private objOwner As ProcessFileObject
Private strFileName As String

Public Sub SetOwner(objRef As ProcessFileObject)
   Set objOwner = objRef
End Sub

Public Sub StartTimer(FileName As String)
   strFileName = FileName
   Timer1.Interval = 55
End Sub

Private Sub Timer1_Timer()
   Timer1.Enabled = False
   objOwner.RecordCount (strFileName)
   Set objOwner = Nothing
   Unload Me
End Sub

Author Comment

ID: 7100313
I tried doing as you said, but that doesn't suffice, actually iam expecting such things
1. There will be a main executable which listens to a socket
2. if an connection request is detected theni will create and object of the ActiveX EXE and ask it to Accept it by passing the discriptor
3.  Then the main executable will be free to take other connection requests
4. The object of ActiveX EXE created in step 2 will be serving a clinet till the client sends a siginal for Termination of Channel
I tried the same with OCX it is happening, but since it is an In Proc server it may create a problem. I tried to do it with ActiveX EXE  but it is not accepting the connection

REquest your advice on the same

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

679 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