Link to home
Start Free TrialLog in
Avatar of DanielBlais
DanielBlaisFlag for Canada

asked on

thread question

Hi,
  I use multiple thread in my application.  I want to know if it is ok.  I want to know if I need to manage multiple pointer.  Those function may take up to 3 or 4 minutes, depending on database and net speed.  Look at my code and tell me what do you think.  And what is going to happen if I receive a lot of query at the same time?  The is a timer that call this function every second.

Do
Fichier = Dir(BaseDir & "request\*.*")
If Fichier <> "" Then
    Select Case UCase(Left(Fichier, 2))
        Case "OR"
            param = BaseDir & "request\" & Fichier
            hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf doOrderRequest, ByVal param, ByVal 0&, lpThreadID)
            closeHandle hThread
        Case "SR"
            param = BaseDir & "request\" & Fichier
            hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf doShipmentRequest, ByVal param, ByVal 0&, lpThreadID)
            closeHandle hThread
        Case "RM"
            param = BaseDir & "request\" & Fichier
            hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf doReceiveMessage, ByVal param, ByVal 0&, lpThreadID)
            closeHandle hThread
Fichier
        Case Else
            fso.movefile BaseDir & "request\" & Fichier, BaseDir & "badrequest\" & Fichier
    End Select
End If
Loop Until Fichier = ""
Avatar of kdotkannan
kdotkannan


I don't know where and how you are using this. But I can give a you a small suggestion if you bother about multiple queries at a same time.

Forward all the queries to a single function, where you can put everything in a que, from there you can execute the request one by one (which are in the que). For this you can use a collection as a que.

Whenever you get query put it in a collection. Mean time your timer can execute the query and remove them from the collection. Don't forget to disable the timer before executing the query (inside the timer function) and start the timer again after executing the query.

This will avoid the problem of missing and handling some of the queries.

..kannan
<ping>.. Listening
VB, though equipped with multithreading, does something very nasty when needing to execute code from the same codebase on different threads. It serializes the requests. This means that you will never see a second call into the same procedure or class module before the first one is finished.

If you want true independant execution have you queries execute in an out of process component (activex exe) and set the class module that will execute the queries for you to Single Use. This will launch a new process for each query. Also, a seperate codebase will be loaded for each process. You must also expedite a quick return from your function call and raise an event when the query is done because otherwise vb simply wait for your query.

DRAWBACK: If your query returns large objects beware of the fact that cross process marshaling is really slow.
ps: drawback2: this also eats memory by the megabyte!
Avatar of DanielBlais

ASKER

fparie, I don't really understand what you mean.

Since I update my code to use thread, it often crash.  Do you now why?  A lot of time, it crash at the first call of an function.
DanielBlais
What i understood from fparie, instead of using a DLL uou better use an EXE with the thread per object threading model. Every time the client will create a new EXE to call your function, the function will be executed and processed in a new thread, thus removing any chances of serializing the process calls that can happen in case of multithreading. However the two drawbacks should be handled carefully, e.g always set the EXE to nothing right after you finish processing etc. This releases the memory the earliest you are finished with the EXE. Just an explainatory comment

Cheers
Here is an example. This program reads 3 text files on 3 different threads.  I added a project reference to Microsoft Scripting Runtime to use File System Objects to access the files. Of course you can use some other method if you prefer.  
 
Timers on forms are used to kick off the threads asynchronously so the files process at the same time. A form is also used to identify the first time though, since main is automatically called every time a thread is created.

Please ask if you have questions. I tried to make it a clean example but I know it isn't easy to follow if you aren't familiar with it. Hope it helps!


==============================================
' MainModule.bas

' 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

Private Sub Form_Initialize()
    Timer1.Interval = 100
End Sub

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

==============================================
' ProcessFileObject.cls

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

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
I want to create an ActiveX DLL that use thread.  It always crash.  How can I do it?
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20162194.html
https://www.experts-exchange.com/questions/Q.20176827.html
https://www.experts-exchange.com/questions/Q.20227696.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
Avatar of DanRollins
DanielBlais, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial