Results 1 to 40 of 54

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

Threaded View

  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

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