Solved

thread question

Posted on 2001-08-02
11
217 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
Comment Utility

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
Comment Utility
<ping>.. Listening
0
 
LVL 1

Expert Comment

by:fparie
Comment Utility
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
Comment Utility
ps: drawback2: this also eats memory by the megabyte!
0
 
LVL 2

Author Comment

by:DanielBlais
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Expert Comment

by:smkkaleem
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Per recommendation, points NOT refunded and question closed.

Netminder
CS Moderator
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now