Page 1 of 2 12 LastLast
Results 1 to 40 of 54

Thread: New code for simulating multithreading in VB6...

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    New code for simulating multithreading in VB6...

    OK, after about 8 months, I have finally got round to finishing off the 2nd revision to my multithreading code I posted here.

    This code is more compact, easier to use and has more functionality than that of the previous code, which was to be fair, a little bit of a nightmare to use unless you were quite compitent at VB.

    Attached to this post is a ZIP file, which contains the multithreading code itself and 2 demo projects on how to use it.

    Some main features of the code:
    • Ability to send data synchronously and asynchronously
    • Send data in any format etc long, string, byte array.
    • When sending data synchronously you can return data for the return value of the send function. Just like a normal function in VB, but across threads.
    • Ability to handle and display errors in the thread or pass them back to the calling app.


    OK, lets get into the code.

    Download the zip file and open it.
    Open the vbGateway2 project and compile to your PC.
    This is the multithreading "engine" and ALL multithreading code is contained in this project. Just 1 DLL...yup that's right

    OK, now go into the Demo folder and open project1 project. Compile this to an EXE. This is a small demo and shows you each function that can be accomplished with this code.

    Once compiled open 2 instances of the EXE.
    Highlight and copy, one of the hWnds in the text box into the other windows child hwnd text box. Notice as soon as you do this both windows captions change to "Connected". The link is made. They can now communicate with each other.

    Now click Send Data. this sends the data in the Send Data textbox to the other thread. Notice how it appears in the listbox. The other thread will wait for 1/2 a second before it responds. This pause is ONLY there to demonstrate that it's been sent synchronously, you can remove this pause and then the thread will reply instantly.

    The post data method is the same, apart from no reponse is required from the thread and so execution resumes in the calling thread. Does that make sense?

    Errors...Right, this is what I have been working on, and it's made life easier in the thread. If an error is raise during a synchronous operation then the error is sent back to the calling thread and a normal error is raised, just like you'd expect if you called a normal function in VB.
    There is also asynchronous error handling provided. Obviously sometimes you may not want your threads to Msgbox errors, espescially if they're hidden and running in the back ground. If a thread is set to handle it's own errors then an event is raised and the error can be displayed, otehrwsie the error is posted back to the calling thread, where it's displayed.

    The project1 demo shows this by either clicking the create error button. Try this and then toggle the check box and try it again. Notice what thread the error is handled in.

    If you try in "ERROR" into the send data text box and click send data then once the data is received in the other thread an error is raised back to the calling thread. This simulates a normal application as if you were calling a normal function in a DLL. very useful.

    This demo shows the basics of the multithreading code.
    Now for a real world example, the same as last time, a file searching demo.

    Open the multithreading folder, and then open the ThreadUI folder. Open the SearchThread project and compile it. This is used for searching files. Many of these threads can be created and multiple searches can take place at the same time. Obviously the more search you have running at the same time the slower your PC will become. This is not the multithreadings codes fault

    Now open the DemoUI folder and open and compile the demoUI project. This is the main UI calling thread and is used to "spawn" many search threads. Once compiled run the EXE. Click Search and a new thread will spawn. While searching the details of found files are displayed in the thread itself and the current file is posted back to the calling thread, the demoUI exe. Lanuch many searches at the same time, this is where you see the full power of multithreading.

    The search code and the demo projects are rough code, obviously all my time and effort has gone into the vbGateway2.dll, which is the backbone of this code.

    Any comments will be much appreciated.

    Hope this code will help you if your app design.

    Wokawidget

    PS I newer release, which is easier to use and has more functionality, is attached to my next post. I left this here for historical reasons.
    Attached Files Attached Files

  2. #2

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    New release with new functionality...

    OK...after a few days of getting annoyed at having to encode and decode the data I sent using the vbGateway2.dll, in my case I used property bags, I decided to write soem code that did this for you.
    I have added 3 classes to the vbGateway2.dll project. this are:
    • Command
    • Parameter
    • Parameters

    These are used to create a command (function) and it's params.
    Say you referenced a DLL in your project and this DLL had the following function in a class called clsUsers:
    VB Code:
    1. Public Function FetchUsers(ByVal TaskCount As Long, ByVal Admin As Boolean) As String 'XML return
    Then in your UI you would do:
    VB Code:
    1. Dim strXML As String
    2. Dim objUsers As clsUsers
    3.    Set objUsers = New clsUsers
    4.    strXML = objUsers.FetchUsers(10, False)
    5.    Set objUsers = Nothing
    This is straight forward, although using the vbGateway2.dll meant you had to somehow convert this into a single bit of data and do:
    VB Code:
    1. Dim strXML As String
    2. Dim varData As Variant
    3.    'create data that the thread can decode and understand
    4.    strXML = mobjGateway.SendData(varData)
    Then is te DataArrived event in the thread you had to decode the data, which can be hard if you are passing lots of encoded parameters.
    This annoyed me, and I'm sure it did some of you
    The new command object deals with this for you.
    So for the FetchUsers example I used above you would now do:
    VB Code:
    1. Private Sub FetchUsers(ByVal plngTaskCount As Long, ByVal plngAdmin As Boolean)
    2. Dim objCommand   As Command
    3.    Set objCommand = New Command
    4.    With objCommand
    5.       .Key = "FetchUsers"
    6.       .Parameters.Add "TaskCount", plngTaskCount
    7.       .Parameters.Add "Admin", pblnAdmin
    8.    End With
    9.    mobjGateway.PostCommand objCommand
    10.    Set objCommand = Nothing
    11. End Sub
    Now this is received by the gateway object in the thread and a new event is raised.
    VB Code:
    1. Private Sub mobjGateway_CommandArrived(Command As vbGateway2.Command, ByVal Synchronous As Boolean)
    What you do now is:
    VB Code:
    1. Private Sub mobjGateway_CommandArrived(Command As vbGateway2.Command, ByVal Synchronous As Boolean)
    2. Dim strXML As String
    3. Dim objRet  As Command
    4.    With Command
    5.       Select Case .Key
    6.          Case "FetchUsers"
    7.             strXML = FetchUsers(.Parameters.Item("TaskCount").Value, .Parameters.Item("Admin").Value
    8.             Set objRet = New Command
    9.             objRet.Key = "UsersFetched"
    10.             objRet.Parameters.Add "XML", strXML
    11.             mobjGateway.PostCommand objRet
    12.             Set objRet = Nothing
    13.       End Select
    14.    End With
    15. End Sub
    16.  
    17. Private Function FetchUsers(ByVal plngTaskCount As Long, ByVal plngAdmin As Boolean) As String
    18.    'code to laod users from DB
    19. End Function
    Then you do pretty much the same thing in the calling thread to handle the return value:
    VB Code:
    1. Private Sub mobjGateway_CommandArrived(Command As vbGateway2.Command, ByVal Synchronous As Boolean)
    2. Dim strXML As String
    3.    With Command
    4.       Select Case .Key
    5.          Case "UsersFetched"
    6.             strXML = .Parameters.Item("XML").Value
    7.            'code to deal with users in XML
    8.       End Select
    9.    End With
    10. End Sub
    Does this make sense.
    I have updated the file searching demo so that it uses this new code. Makes things MUCH MCUH easier There is also a new button on the Main UI screen called Cancel ALL searches. This sends a cancel command to ALL threads and terminates their search. This shows how simple it is to send and handle many different command, with or without parameters.

    The above exmaple was how to load users Asynchronously...to do it synchronously you would do the following in the UI:
    VB Code:
    1. Private Sub FetchUsers(ByVal plngTaskCount As Long, ByVal plngAdmin As Boolean)
    2. Dim strXML As String
    3. Dim objCommand   As Command
    4.    Set objCommand = New Command
    5.    With objCommand
    6.       .Key = "FetchUsers"
    7.       .Parameters.Add "TaskCount", plngTaskCount
    8.       .Parameters.Add "Admin", pblnAdmin
    9.    End With
    10.    strXML = mobjGateway.SendCommand(objCommand)
    11.    Set objCommand = Nothing
    12. End Sub
    Then in the thread you would have:
    VB Code:
    1. Private Sub mobjGateway_CommandArrived(Command As vbGateway2.Command, ByVal Synchronous As Boolean)
    2. Dim strXML As String
    3. Dim objRet  As Command
    4.    With Command
    5.       Select Case .Key
    6.          Case "FetchUsers"
    7.             strXML = FetchUsers(.Parameters.Item("TaskCount").Value, .Parameters.Item("Admin").Value
    8.             mobjGateway.Reply = strXML
    9.       End Select
    10.    End With
    11. End Sub
    Even easier

    There is also a VERY simple demo of this in the root folder of the vbGateway2 project.
    Just load Test.vpg group project and run it.
    This simply has 2 buttons, send synchronously and send Asynchronously. This shows you a very simple example.

    Just follow the same steps as in the 1st post.

    Download the zip file and open it.
    Open the vbGateway2 project and compile to your PC

    Open the multithreading folder, and then open the ThreadUI folder. Open the SearchThread project and compile it. This is used for searching files. Many of these threads can be created and multiple searches can take place at the same time. Obviously the more search you have running at the same time the slower your PC will become. This is not the multithreadings codes fault

    If you DO NOT enter a file extension and you have many search threads open then your UI will become unresponsive. this is expected, since the amount of files, and their details, that are being sent to the client is bloody LOADS! and ANY app will become unresponse when dealing with this much data...again, this is not the codes fault.

    I hope this helps.

    Woof
    Attached Files Attached Files

  3. #3
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: New release with new functionality...

    Are you ever going to put a seal on this damn project ?

    This code sucks badger

    Only cause I can only understand 1/3 of it
    hahahahahah

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sorry it took me so long - I had to install VB6.

    Pretty damn good! I tried the searching example, the Project1 child hWnd example, and the just generally checked out the gateway code. It likes like the searcher objects are trying to simulate inheritance of the Gateway object. Pretty clever stuff with handling creating a window and managing its messages.

    I hardly do any VB6 work anymore but I tip my hat to ya.

  5. #5
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Again, I apologize for bringing up an old thread, but I was wondering...

    Can you have more than 2 applications that work in a multi-threaded state?

    I am planning on having 1 main ActiveX application that has a single class (set to 'multi-use') and multiple instances of another smaller activeX application.

    I want the multiple instances of the smaller activeX EXEs to communicate with the main application. From your demo it only looks like you can have 2 applications connected together.

    (p.s. I haven’t had time to look at it in full, so correct me if I’m wrong! It looks very good).

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Old threads are here to be brough up

    Only 2? Where did you gte the value of 2 from?
    The demo in the zip is for searching for files...you can start many searches at the same time can you not, and not just 2...???

    Woka

  7. #7
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    In the demo that I am using there are only two applications, and they alk to each other. One says Sausages, and the other says Growl. Dont worry about that though, I've worked it out now (I've just taken I more in-depth look at it).

    EDIT:
    Oh yeah, Im trying to impiment Winsockets into this code (for testing purpoises). Basically I am just using the Demo Application (The Sausage/Growl one) and I have put a Winsock on the form.

    When you click a button on one, the winsock starts to listen on port 100. Then, when it retreives a connection request, it ofrwards the RequestID to the other thread...

    Code:
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    mobjGateway.SendData (requestID)
    End Sub
    Then, the other thread attempts to accept the connection request...

    Code:
    Private Sub mobjGateway_DataArrived(Data As Variant, ByVal Synchronous As Boolean)
    Dim bytReturn()     As Byte
    On Error GoTo ErrHandler
    
        Winsock1.Close
        Winsock1.Accept CLng(Data)
    
        Exit Sub
    ErrHandler:
        mobjGateway.RaiseError Err.Number, Err.Source, Err.Description
    End Sub
    BUT when I try to connect to the application via telnet, the code gets to 'Winsock1.Accept CLng(Data)', and crashes. No runtime errors or anything, it jsut crashes and leaves the Windows XP 'Report Error' dialoge box.

    Any idea why its doing this? MSDN says that you can use winsock accross seperate threads, so it should work (however, it doesnt say anything about doing it accross seperate processes).
    Last edited by lozware; Apr 25th, 2006 at 07:53 AM.

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Hahaha, yea, the sausage and growl example

    But there should be another demo in that zip. One for searching for files on your PC...?

    Woka

  9. #9
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    haha, ok.

    But did you take a chance to look at my other problem - its in my last post (I editted it in, its to do with using Winsock).

  10. #10

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    This cannot be done due to it being in a different thread. This was explained in CVMichaels winsock thread.
    To pass the requestID to another winsock control they must be running in the same process, in this case, they are not, and thus it will not work. U have to start a new thread to listen for connections.

    Woka

  11. #11
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    AAAAAAAAARRGGGGGGGGGHHHHH!!!!!!!!

    Thanks anyway, nice code.

    (p.s. Why did you suggest using this code if you knew that it wouldnt work?!?!)

  12. #12

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Beacuse...if you read the other thread I explain
    U create a NEW thread that listens for incoming connections, and the one that took the RequestID, connects to that request.

    Woka

  13. #13
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    But how is that different from what I am doing at the moment? At the mo I have an ActiveX EXE that listens for connection,s nd accepts them. Then, when it does accept a new connection, it loads another instance of itself.

  14. #14

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    well that will work then
    What u cannot do is PASS the requestID to ANOTHER process. This will not work.

    Woka

  15. #15
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    It does work, but not very well... this is why I seeked guidance in the first place.

    There is a delay between the time that the new connection is accepted, and the time it takes for the new process to start listening. This means that under intense traffic, a lot of connections are not accepted.

    Ah well, I will just keep on working on alternative approaches

  16. #16
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Whooooo! I just learned how to multithread properly (i.e. Multiple threads under just 1 process). Its absolutely amazing, but I’m still having a bit of grief with the ol' Winsock... I’m getting pretty damn close though!

  17. #17

  18. #18
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Ok, here we go...

    1) Create a New ActiveX EXE project.

    2) Go into Properties, and select 'Thread per Object' (under the General tab), and set the Start-up object to 'Sub Main'.

    3) Then click on the Component tab, and set the Start Mode to 'Standalone'.

    4) Make 2 forms - one is your main form, and the other will be your thread (the one that you want to create multiple instances of). We will call the main form frmMain, and the thread form frmThread.

    5) Make a new module, and put the following code into it...
    VB Code:
    1. 'Module1 Code----
    2. Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    3. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    6. Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    7. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    8. Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    9. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    10. Public Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    11.  
    12. Sub Main()
    13.  
    14.     Dim ProcessID As Long, curProcessID As Long
    15.     hwnd = FindWindow(vbNullString, "Put the Main Form's Caption Here!")
    16.    
    17.     If hwnd <> 0 Then
    18.         GetWindowThreadProcessId hwnd, ProcessID
    19.         curProcessID = GetCurrentProcessId
    20.        
    21.         If curProcessID <> ProcessID Then
    22.             Dim Frm As New frmMain
    23.                 Frm.Show
    24.             Set Frm = Nothing
    25.  
    26.         End If
    27.     End If
    28.      
    29.              
    30.    If hwnd = 0 Then
    31.             Dim Frm2 As New frmMain
    32.                 Frm2.Show
    33.             Set Frm2 = Nothing
    34.     End If
    35. End Sub

    6) Make a new Class Module, call it Thread, and put the following code into it...
    VB Code:
    1. 'Project1.Thread Code----
    2. Sub NewFormThread(OwnerObject as Object, MirrorObject as object)
    3.    
    4.     Dim Frm As New frmThread
    5.    
    6.     Frm.Show
    7.     Set MirrorObject = Frm
    8.     Set Frm.OwnerObject = OwnerObject
    9.     Set Frm = Nothing
    10.  
    11. End Sub

    7) (we're getting there!) In frmThread, create a Public Object variable (notice how the above function requires the form to have one)...
    VB Code:
    1. 'frmThread Code----
    2. Public OwnerObject as object
    This object is going to be how the thread communicates with the main form (frmMain).

    8) You're all set to go! When you start the project, the main form will load (provided you made the correct changes to the Sub Main). Then, whenever you want to load a new thread, put this in your code:
    VB Code:
    1. 'frmMain Code----
    2. Dim Thready As Object
    3. Dim F As Form, tF As Form
    4. Dim Obj As Project1.Thread
    5.  
    6. Set Obj = CreateObject("Project1.Thread")
    7. Call Obj.NewFormThread(Me, Thready)
    8. Set Obj = Nothing
    9.  
    10. Thready.Winsock.Close               ' Just an example of how to use the
    11. Thready.Winsock.LocalPort = 23      ' "MirrorObject" to communicate with the
    12. Thready.Winsock.Listen              ' thread after it has been created.

    There you go ladies and gentleman, that is TRUE multithreading! If you want, here is sometihng cool that you can do on the frmThread form so that you know that it is working properly...
    VB Code:
    1. 'FrmThread Code----
    2. Private Sub Form_Load()
    3.  
    4. me.Caption = App.ThreadID
    5.  
    6. End Sub


    (NOTE: The above code was originally written by Srideep Prasad, all I did was edit it to suit what I needed, and comment on it (duh!))

    (NOTE #2: You must run this as a compiled .EXE - the multithreading wont work in the VB IDE environment)
    Last edited by lozware; May 2nd, 2006 at 01:40 AM.

  19. #19

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    No it's not

    U honestly think I went to all that trouble to write multithreading code if it were that simple

    There is one HUGE flaw with your code.

    Say you want to call a function in your activeX control:
    VB Code:
    1. Public Sub DoSomething()
    2. Dim lngIndex As Long
    3.    For lngIndex = 0 To 10000
    4.       'code here
    5.    Loop
    6. End SUb
    Then when you call this from your UI, the code execution of the UI will stop UNTIL that function has finished. This is bad as it locks the UI and the other threads can therefore no longer raise events to it.
    Ok, so it is possible to get around this by using a timer:
    VB Code:
    1. Public Sub DoSomething()
    2.    tmrEvent.Enabled = True
    3. End Sub
    4.  
    5. Private Sub tmrEvent_Timer()
    6. Dim lngIndex As Long  
    7.  
    8.    tmrEvent.Enabled = True
    9.  
    10.    For lngIndex = 0 To 10000
    11.       'code here
    12.    Loop
    13. End Sub
    Ok, so this is good. Or soooooo you think.

    Lets take the following.
    Your ActiveX control is doing something, lets say it's processing some data from winsock.
    VB Code:
    1. Private Sub ProcessData(ByVal pstrData As String)
    2.     'code to do something complicated with data
    3. End Sub
    This issue arrives here when the UI tries to communicate with the ActiveX EXE is anyway! Lets say you have a timer on your form that updates the status of the winsock connections in a grid:
    VB Code:
    1. Private Sub tmrStatus_Timer()
    2. Dim obj As Thread
    3.     'code to get one ActiveX EXE
    4.    
    5.    lblStatus.Caption = obj.BytesProcessed & " bytes"
    6. End Sub
    Now, whe the UI tries to get BytesProcessed you get a lovely "Switch To, Retry" error. Very annoying, very irritating, and it's simpley not good in your app.

    This is where my code comes in I can communicate with the ActiveX EXE's regardless if they are busy or not, and vis versa. Using your method you cannot.

    Almost multithreading, but your code falls at the last hurdle. Trust me on this one.

    Woka

  20. #20
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Where abouts are you saying it would freeze up? If I were to put that loop in one of the threads it would not freeze up - I can give you a working example if you want.

  21. #21

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    If your thread is "doing something" and u tried to access a property of that thread, or try to call a method from that thread. Try it.

    On a form in your thread add a button and the do:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngIndex As Long
    3.     For lngIndex = 1 To 100000
    4.         Command1.Caption = CStr(lngIndex)
    5.     Next lngIndex
    6. End Sub
    Also, to your thread class add the following:
    VB Code:
    1. Public Property Get SomeText() As String
    2.    SomeText = "Woof"
    3. End Property
    Run your code.
    Make the thread do something by clicking the button.
    Now, your UI is still active and will not hang. This is good.
    But while your thread is doing something try and call the thread property SomeText from the UI.
    Your app WILL fall over.
    This is the last thing you want for this app.

    Like I said, trust me on this one, well no, in fact don't trust me, try it yourself. This will happen. You WILL get the "Switch To...Retry" error.

    Your app cannot, and will not, be able to communicate with the thread UNTIL that loop has finished.
    A very very quick fix would be do add DoEvents into the loop, but this is VERY VERY bad and can mess up certain code execution and leave things in memory. This should not be used.

    Do you honestly think I would have spent all my time and effort in writting my vbGateway code if what you're suggesting is possible? I've been down that route and tried and tested it all many many times to perfect my vbGateway code.

    Woka

  22. #22
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Well yeah, that’s kinda pointing out the obvious. A thread is just that... a thread. If any thread is busy (whether it was written in C++ or VB), then it wont respond. If you wanted to have the thread readable while performing a loop, then you would need to split that form up into 2 threads, one to do the Do While Loop, and one to host the properties (thats just plain common-sense).

    Look at the attached file. open it up, and compile the EXE. It creates threads that go into Big DO WHILE loops, and reads from them at the same time!!!
    Attached Files Attached Files
    Last edited by lozware; May 2nd, 2006 at 05:42 AM.

  23. #23

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    I don't need to create another thread to read from mine.
    I don't have VB6 on this PC, but I've looked at your code, and it's like what I said.
    It's almost multithreading, but like I said, it will fall over if you try to make a call to the thread when it's doing something.
    This imo is a fundamental floor and is therefore not multithreading.

    The last thing you want to for this app to be running on the server and for you to run into these problems.

    Seriously, I know what you're trying to do, and I know it "almost works", but it's just not good enough for a server app imo as you cannot make calls to a thread that is doing something.
    In VB.NET and c# you can make calls to a thread that is doing something.

    VB6 does NOT do true proper multithreading, regardless of what people say, it's impossible in VB6. The ONLY way around this is to use code that I have written.
    Yea, there are many variations on this, which you have found, but they are not as good as the code I wrote as I can call into a thread that is working and my app won't hang, or show that "Switch to" error due to the way I have coded how the UI and thread interact with each other. This is where all my work and dev time went into, to get around this issue of calling a working thread.

    Use your code if you want, but bear in mind that it only 1/2 simulates multithreading and you will/may have issues when trying to communicate with a working thread.

    Woof

    Woka

  24. #24
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    IMO I prefer my method. If you need to read a property from the busy thread, then make a new thread to do so... like I said, it makes sense. If you actually run the compiled EXE of the project I attached, you will actually see just one process in the task manager, and each time you press the 'Create New Thread' button, the thread number will go up for that process (you need to go View> SelectColumns > Threads to see this). So to be honest, I would class this as real multithreading... 1 process... multiple threads... what else does it need to do in order to classify as multithreading?!?

    However, on your program all it does is create multiple processes, and link them together via a DLL - which is far from multithreading, and it means that you are very limited in what you can do (like passing Connection Requests from Winsockets). Apart from the Winsock limitation, you are also burning up huge amounts of memory.

  25. #25

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Hahaa. Ok. fair enough. We will have to disagree to agree on this one.
    But one of the main points regarding multithreading is being able to make calls to it while it's busy

    I don't need to compile and see your code. I've seen the same thing a million squillion times before. I did exactly what u have done when I 1st started out doing this...it's not new code, it's all over the web. However, I absolutely 100% needed to be able to call into that thread to get access to functions just as "CloseConnection" regardless if it's doing something or not. You cannot do this, even with your use of another thread. Your code WILL crash here I am afraid.
    If you close your main UI app down while you have many threads open, and u don't call CloseThread function in the thread then because you are using hidden forms I believe this will cause a memory leak.

    I have done this code a million times over many numbers of years and have seen the pro's and conn's of all methods.

    Woka

  26. #26
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    I still think you should give it a go, you havent seen it running yet. I know you have seen the code in the ZIP file, but unless you are either super-naturally clever, or have some sort of built-in VB compiler in you head, then you CANT picture it working without compiling it and running it.

    EDIT:
    And apart from anything, its just rude. It took me 30 mins to put the first lot of code up here, and another 30 mins to write the test project (which was tailor made for you).

  27. #27
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: New code for simulating multithreading in VB6...

    Like many long-term members here, Woka is capable of knowing how code will work by just reading the code. When you have lots of programming experience, you dont need to compile code to know what it is doing, or (roughly) how efficiently it will do it.

    As far as (simulated) multi-threading for VB6 goes, I've never come across anyone with as much knowledge as Woka.

    As far as writing of the code for his benefit, there wasnt really any point I'm afraid - as he had mentioned that he'd already done something similar. It was presumably a learning experience for you tho, so it's not that bad.

  28. #28
    Junior Member
    Join Date
    Apr 2006
    Posts
    17

    Re: New code for simulating multithreading in VB6...

    Right - I am sorry, but I wasn't aware that my knowledge in VB and multithreading was reflected in my post-count.

    si_the_geek:
    Don't patronise me.

    Wokka:
    Good luck with your code, but I will use mine thanks - and it is working very well.

  29. #29
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: New code for simulating multithreading in VB6...

    I didn't mean it to be patronising, I'm sorry it came across that way. I never mentioned anything about your post count - I have never seen someone with as much knowledge as Woka on this subject, even after some serious searching the web for alternatives.

    I can see from what you have posted that you have good knowledge of it too (probably better than me), but that doesn't change the fact that Woka's view comes from lots experience, and his analysis is likely to be correct. If your code works for you tho, that's great. I'm sure others will be able to learn from it.

  30. #30

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    I have no problem at all with you shunning my code, your code to be fair deals with the RequestID of winsock much better than mine does, which is a big bonus in your case, but my code handles calling into the thread much much better than yours. It's a trade off of what you want in your app. I was just trying to make you aware that you may run into issues with your threads.

    Si wasn't patronising you in the slightest. The code you have posted on here is very simple, and like Si mentioned, I don't need a compiler to see how it runs, I can just view the raw code in notepad. I am sorry you took his comments that way

    I am doing consultancy work in Norway at the moment, and don't have VB6 on my lappy, nor do have have direct access to the VB6 install CD's. I do at home though.
    If I did have VB6 I would have compiled your code, modified it slightly and reposted it to show exactly what I have been getting at. Just for reference purposes mind you.

    Your code, although it runs in seperate threads quite happily, is missing quite a lot of "important" multithreading capablities/functionality, which if you don't need then this is fine.

    No one here takes your post count into concideration at all. Everyone has to start at a post count of 0 at some point, regardless of their programming skills.

    Si was just trying to say that some ppl don't need to compile and test code to see how it works, so it's not rude of me really, which I think Si was trying to point out. I would if I had VB6, but then again, I would gain no extra benefit from compiling and testing your code, unless I was going to modify it and post it back to you...even then, notepad would suffice to a certain extent.

    Trust me, no one is insulting your intelligence...

    woka

  31. #31
    New Member
    Join Date
    May 2006
    Posts
    1

    Re: New code for simulating multithreading in VB6...

    I have gone through your code for multithreading. It is indeed a very good code. I came across your codes when searching for multithreading with VB6.

    I was not aware of a proper method to do multithreading in VB6 so I chose to use VB.NET to do multithreading.

    In my application, I use two threads in the single application. One thread is used to run the main process. While the process is running in background, a form is displayed with a button STOP for stopping the process. As soon as the user presses the STOP button, the first thread running the main process is terminated.

    This is easily achievable in VB.NET. Can this be done taking help of your code.

    Kaushik Kale.

  32. #32

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Yes this is easily do-able in .NET, ver y easy.
    And yes, my code can do what you've described.
    Just create 2 thread sin the UI app.

    WOka

  33. #33
    New Member hanysaad's Avatar
    Join Date
    Jan 2006
    Location
    Egypt , Asyut
    Posts
    5

    Re: New code for simulating multithreading in VB6...

    Thank you !!!
    very nice !
    "THERE IS NO GOD EXCEPT ALLAH, AND MUHAMMED IS HIS MESSENGER "
    Hany Saad Moustaffa .
    Moderator of VB6.0 forum in Arabteam forums.
    Egypt, Asyut.
    Asyut university, faculty of science.
    E-mail : hanysaad_2000@yahoo.com
    My Blog : http://hanysaad.blogspot.com/

  34. #34
    Addicted Member
    Join Date
    Jul 2001
    Posts
    133

    Re: New code for simulating multithreading in VB6...

    Quote Originally Posted by Wokawidget
    No it's not
    Yes it was :-)

    Quote Originally Posted by Wokawidget
    U honestly think I went to all that trouble to write multithreading code if it were that simple
    You *did not* write multithreading code ... you wrote a messaging front end.

    In fact, your sample code is NOT (I repeat NOT) multithreading .... it's multiple processes. My dear friend, they are not the same things!

    Quote Originally Posted by Wokawidget
    This is where my code comes in I can communicate with the ActiveX EXE's regardless if they are busy or not, and vis versa. Using your method you cannot.
    No, you can not! A busy thread is a busy thread! *** What YOU DO MEAN is that the caller won't BLOCK. ***

    Your just avoiding the block by avoiding the interface (which is marshalled).

    However, to be certain, you are NOT talking to a busy thread - assuming the thread is busy with something OTHER than your IPC mechanism.

    I have looked at and tested you code.

    With due respect: Your using Windows Messaging for an IPC mechanism ... Yuch!

    Sorry, but my IPC is way faster and doesn't eat the system.

    I also noticed that after you start a couple seaches, your main form pukes and becomes unresponsive.

    Quote Originally Posted by Wokawidget
    Almost multithreading, but your code falls at the last hurdle. Trust me on this one.
    Woka
    No, don't!

    I was "harsh" - Iozware made valuable points which were overlooked.

    What Woka has created is an IPC mechanism ... *not* multithreading. Basically, using the Windows message system to avoid VB mashalling which kicks in when the public interface is called.

    Woka, that's NOT mulithreading MAN! You have an IPC.

    THIS DISCUSSION has been about TWO topics which are being called one and the being confused

    And yes, you can reliably INPROC multithread with VB6.
    Last edited by Jay Rogozinsky; Mar 16th, 2007 at 03:52 AM.

  35. #35

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: New code for simulating multithreading in VB6...

    Hmmmm. I am trying to find the thread where you have quoted me there. Can you link it.
    The title of this thread has the word "Simulating" in it if you hadn't noticed, and I am fully aware it uses processes.

    My main form is only a demo. And it's not that that's causing it to be unresponsive. You make silly abounts of calls to any UI and it'll become unresponsive. I did not code for that in the simple demo.

    Woka

  36. #36
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Re: New code for simulating multithreading in VB6...

    I thought posting EXEs in your zips was prohibted?
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  37. #37
    Junior Member
    Join Date
    Jul 2008
    Posts
    23

    Re: New code for simulating multithreading in VB6...

    Wokawidget: Thank you for providing the protocols enabling the communication required for a sort of multithreading, although it would be nice to be able to have a common data source and proper multithreading within a single program as I used to be able to do when writing in Coral and Assembler.

    Perhaps I'm using the wrong language, but I've only recently returned to programming and haven't got round to learning C yet, although have written over 40,000 lines of VB in one project and nearly as much in my second.

    Thanks.

    Chris.

  38. #38
    Junior Member
    Join Date
    May 2008
    Posts
    24

    Re: New code for simulating multithreading in VB6...

    Sir,how come the demoui doesn't work in my computer?It says that activex can't create object.Thanks a lot..

  39. #39
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: New code for simulating multithreading in VB6...

    Have you compiled the DLL?

  40. #40
    Junior Member
    Join Date
    May 2008
    Posts
    24

    Re: New code for simulating multithreading in VB6...

    I've compiled thee dll file..What should i fill in the extention textbox?Thanks

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width