Solved

want to muti thread a Socket Prorgrame in VB

Posted on 2002-05-20
7
628 Views
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 )
0
Comment
Question by:vinaikk
7 Comments
 
LVL 3

Accepted Solution

by:
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.  
0
 
LVL 1

Expert Comment

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

Expert Comment

by:EDDYKT
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:vinaikk
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
vinai.karunakaran@ext.icicibank.com

will be waiting for your reply, it is quiet urgent

Regards
Vinai KK
0
 
LVL 3

Expert Comment

by:adg
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.
0
 
LVL 3

Expert Comment

by:adg
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
   CreateThreads
   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)
   ts.ReadAll
   ReportRecordCount FileName, ts.Line
   ts.Close
   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
0
 

Author Comment

by:vinaikk
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now