Solved

thread question

Posted on 2001-08-02
11
218 Views
Last Modified: 2012-05-04
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 = ""
0
Comment
Question by:DanielBlais
11 Comments
 
LVL 3

Expert Comment

by:kdotkannan
ID: 6344524

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
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6344591
<ping>.. Listening
0
 
LVL 1

Expert Comment

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

Expert Comment

by:fparie
ID: 6344992
ps: drawback2: this also eats memory by the megabyte!
0
 
LVL 2

Author Comment

by:DanielBlais
ID: 6345458
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:smkkaleem
ID: 6346297
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
0
 
LVL 3

Expert Comment

by:adg
ID: 6346374
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
0
 
LVL 2

Author Comment

by:DanielBlais
ID: 6598416
I want to create an ActiveX DLL that use thread.  It always crash.  How can I do it?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6908193
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.
http://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.  http://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.
http://www.experts-exchange.com/questions/Q.20162194.html
http://www.experts-exchange.com/questions/Q.20176827.html
http://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 http://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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7198429
DanielBlais, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 7213317
Per recommendation, points NOT refunded and question closed.

Netminder
CS Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input past end of file vbs script 9 82
Excel VBA combine two working workbooks 8 47
VB6 ListBox Question 4 33
Advice in Xamarin 21 57
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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