I have this kept this thread open for reference purposes, but for the new and improved multithreading please see the link above.
Cheers,
Woka
The attached project demonstrates how to achieve multithreading in VB6.
The example is only a concept example and merely adds 2 numbers togther, but delays the result by 10 seconds as to simulate code that would take up more processing power.
You will need to compile the vbTools project 1st, and both the ThreadProxy and the Thread project use core functions from this DLL.
Once this has been compiled then the Thread project needs compiling.
After you have compiled these 2 projects then you can launch the Demo group in VB and run the application.
I appologise for the lack of comments, but it should be pretty straight forward apart from the MemoryFunctions in vbTools2.
If you have any comments then give me a shout.
Regards,
Woka
Last edited by Wokawidget; Oct 15th, 2004 at 04:03 AM.
You can't, I don't think, but you can debug one thread. When you are happy, compile it and then run your app while referencing the ActiveX EXE instead of the VB project.
Right, here's a much more realistic demo.
The main app spawns new file search windows.
You can search all directories inside the given path, and search for a certain file extension. If the file extension is blank then the app lists all files.
Each thread that is searching for files, relays the info that it has got back to the main app. This could be used or say threads that are downloading data and want to display the progress in a grid in the main app.
The main app here displays how many files, and what the last file was that was found.
If the thread window is closed then the object is also destroyed from the main app. Also, if the main app is closed then ALL the threads are destroyed.
This is a better example of passing many varibles between the threads in one call.
Again, you need to compile vbTools2 first.
Then compile the Thread Project, requires vbTools2.
Then run the demo group. The ThreadProxy project in this group requires vbTool2.
Comments are most welcome.
Woka
Last edited by Wokawidget; Oct 24th, 2003 at 08:18 AM.
Not working for me. I compiled vbtool2 and ThreadProxy, and ran the Demo group project. it of course gave me errors of such a magnitude that one could only attribute it to an insane badger. Oh wait a minute...
Anyways, I got past that by manually referencing the mythread.exe and vbtool2.dll files, but "No public creatable component was detected."
It's early in the morning, and I am not motivated enough to fix it myself.
For soem reason the downlaoded project failed to find some classes. Boooo.
Anyways, I have made some small alterations...one of which being the search code now no longer errors when it comes accross a file like pagesys
OK...download the project in my last post.
Compile vbTools2
Your ThreadProxy project has a simple module, not a class module, and you're trying to compile a DLL out of it. Nincompoopy badger, DLLs require CLASS modules!!
Now I shall have to expend the effort of converting that into a Class Module.
Hahaha. Cheers.
That error handler won't work.
The path error will be found in the Thread object and will not be transfered back to the client, unless you create an Error type and pass an ENCOUNTERED_ERROR windows message back to the client, and then handle this in the subclassing sub.
OK, tell a lie...it would be the GetAttr function that causes the error and not the ThreadProxy, which is what i was thinking. I was thinking furtehr ahead
Can't believe you're having a go at my coding! Hahahahaha
Any more whinging from you and I will officially demote you to Slop Bagder.
OK, have uploaded the project, complete with error handler.
I have used:
VB Code:
Private Sub cmdNew_Click()
On Error GoTo ErrHandler
If (GetAttr(Trim$(txtPath.Text)) And vbDirectory) = vbDirectory Then
I have compiled your application, first thing i did was compile vbtools2, then ThreadProxy and then Thread and finally i lanched demo group and run it and it worked, but when in the first form(form1) i click then Start Loop button and i move this window with the mouse the Loop Count stop, why it happened????
I think your application is good.
So i need your help cause i need to make an application which it has to do the next task.
This application actually runs a stored procedure n times, but it do it in a loop, the problem is that this stored procedure take several minutes and i need that this stored runs 2 o 3 with differents parameters at the same time, i know that it is possible with miltithreading, i have tried the API CreateThread but it crashes, in addition i've read that it is not very reliable,
Also i download and kind of Thread emulator, and running a Stored procedure the most of the times the stored procedure didn´t finish its task, i.e. the stored procedure had to insert 28 registers and sometimes it just inserted 7 or 4 or 9 register, it was strange,
So how could i use this component, i'd really need some kind of documantation as a guide.
That is because when you try and resize/move the form it halts the thread. That happens with ALL applications if I am not mistaken.
If you download my 2nd zipped example above then it will show you in more detail how to pass many varibles to the thread. this would help you in passing varibles to your SP...and returning multiple varibles back to the UI.
CreateThread API does not work under VB
MS suck Hahahaha [Only Kidding]
1) What Retry Error? I have just clicked the new button like mad and created about 50 new threads in about 10 seconds...All loaded and started searching for files perfectly...
There should be no need to disable the button...explain.
2) The form flickers is not actually the form, it's the listview. this is because when a thread passes data to the Ui and the UI updates the listview columns, it automatically refreshes itself. This cannot be stopped with a listview (to my knowledge), and to be fair it is getting hammered with updates if you have many threads open and are searching for ALL files
3)When you have LOADS of threads open the overall speed of your processor is compromised. this starts slowing ALL processes down on your PC...if you had an unrelated app running then this would also start to slow...that is due to the OS. You can demo this by attempting to open 30 MS Windows Search things and getting them to all search at the same time.
Right, enough of my whinging
This type of programming is perfect for say multiple download programs liek DAP and such like, but can have many, mnay benefits in other applications.
Glad you like it.
Should I copyright the code? Or don't I have a leg to stand on? hahaha
OH... so suddenly MY code isn't good enough for you? Do I smell... JEALOUSY?
Anyways, don't copyright it. Else I'll personally come over to your filthy little hamlet and kick your edited by MartinLiss, even though he supports this idea as well .
Your code was fine. It's just i have a thing about GOTO commands. I ONLY use them for error handlers and not to pass the execution of code to another point in the sub/function...
I always have a generic errorhandler then distiguish between different err.numbers to decide what action to take.
That sounded far too serious
Hahahahaha
Growly Woof Woofs...I was not fit to be god enough to forget the error handler all together, and for that I am grateful...now go and hide in a long dark cancal and try to mate with a rummpy toad under a full moon Frrroooaaaaarrr.
I bet u need a fusion powered shuttle to reach my place...
Posts
963
Cool Woka. My best compliments to u.
I've d/l ur project and it worked gr8.
But tryin' 2 figure things out of that massive codes is a hard one.
U dont seem to use outprocess threading, neither do u use
interprocess threading either. But they really multi-thread!
it seems u rely on windows messaging? but how?
anyway, pls input the pseudocodes or method u use.
it'll be gr8
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people
I bet u need a fusion powered shuttle to reach my place...
Posts
963
Woka,
Are u using a proxy thread?
Ironically, though it's pretty self-explainatory through ur
ThreadProxy project, well, ......stupid me
because i really can't understand how ur codes do that...
initially i thought it involves some playing with windows
messaging? because ur codes dont seem to play with thread-like
stuffs...u know...like CreateThread,CreateObject,SetTimer
One question for u though, does ur threads live in different
apartments?
I recently visit some multi-threading sites at desaware's...
and they demonstrate apartment threading using Proxy Threading
and it works very well, albeit some performance trade off due to
marshalling...
EDIT: Duh...I mean works very well on VB5
Last edited by jian2587; Oct 26th, 2003 at 09:43 AM.
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people
The thread proxy creates a new instance of the ActiveX EXE, which runs in a seperate thread to the calling project, and runs in a seperate process.
Using the postmessage API I send the uMsg and the memory location of the UDT to the ActiveX EXE, returning execution straight back to the threadproxy, and in turn the UI.
Once the ActiveX EXE recieves the message it reads the memory from the ThreadProxy process. This is how the varibles are passed. Once it has recieved the varibles it the does whatever work needs to be done...running in a seperate thread.
Exactly the same is used to pass info back to the UI.
Make sense?
Sorry, not that good at explaining things am I
Doesn't help that I am suffering from a great weekend of parties and clubs...have had 5 hrs kip since Friday morning
The ThreadProxy project is merely to allow ease of coding in the UI's that u wish to utilise the functionality. This code could just as easily be placed in the UI itself, but splitting it into a standard DLL makes code easier to maintain and read.
Whats multi-threading and what does it do/whats it for?
In multithreading, a single process can call several calls. Or you can look at it another way and say that it's the simultaneious execution of several function flows or "threads".
This is possible if these threads are not dependent upon one another. Since these threads aren't dependent upon one another, it's advantageous to have several different portions of the programs being executed in the most optimum way, instead of having them execute in a serial fashion. I can give you a very small example here:
Let's say we're to evaluate the following expression
a=92
b = a + 382
c = a*b + a^2
d = 283+ 28+a
x = a^2 + 2bc + c^2
Now if we were to implement multithreading on this, you can easily see that d is independent of b, c and x. "a" is a constant in it, so while the evaluation of b and c are going on, we can get the "d" thread to work, evaluate d, and after b and c get evaluated, we plug them into x.
This is a redundant example because you wouldn't notice the performance difference on something like this. But there are areas and applications where multithreading would be of good use to you.
I bet u need a fusion powered shuttle to reach my place...
Posts
963
Woka,
about that Proxy thread thing...
well, because i've seen some multithreading techniques involving
the use of IDispatch interface to act as the proxy for the thread
u want to create. So while it does thread, it slows down because
of that extra marshalling. Not compatible with VB6, though.
anyway, u did a good job and good explaination. albeit using
another process to thread...sigh...that's VB6...
well,u're da man!
I'll see if we can get things better through this technique.
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people
There is one tiny weeny ickle glitch with my code. Has anyone spotted it yet?
I am currently working on a newer improved version. Well in my head anyways. The concept will work and it will make the multithreading more reliable and glitch free. I'll get round to writting it in the next few days. My head's still mashed from the wkend
Don't suppose you could post links to the multithreading you have seen. I have never seen any other code to try at achieve what I am moving towards.
Woka
Last edited by Wokawidget; Oct 27th, 2003 at 04:16 AM.
1) What Retry Error? I have just clicked the new button like mad and created about 50 new threads in about 10 seconds...All loaded and started searching for files perfectly...
There should be no need to disable the button...explain.
I only needed to get up to 4 threads for it to happen, I think it was taking a while to spark the threads off (I'm using a very slow PC - 500Mhz, 128MB )... haven't investigated it to find out where the issue is tho.
2) The form flickers is not actually the form, it's the listview. this is because when a thread passes data to the Ui and the UI updates the listview columns, it automatically refreshes itself. This cannot be stopped with a listview (to my knowledge), and to be fair it is getting hammered with updates if you have many threads open and are searching for ALL files
3)When you have LOADS of threads open the overall speed of your processor is compromised. this starts slowing ALL processes down on your PC...if you had an unrelated app running then this would also start to slow...that is due to the OS. You can demo this by attempting to open 30 MS Windows Search things and getting them to all search at the same time.
all agreed - not important issues at all, I was just being pedantic
Should I copyright the code? Or don't I have a leg to stand on? hahaha
Woka
Shouldn't that be 4 legs?
Now it's on the web I'm not sure if you could, especially with the AUP of this site (not that I know much about it...). If you created a Dll or whatever with the concepts of this in then it should be fine.
OK, I know where this is happening...it's when you create the ActiveX EXE class in the threadproxy and access 2 of it's properties. This can easily be resolved. I will add this into the new revision. I can't force this on my PC as it's a very fast one.
I too was being pedantic hahahaa
Only have 3 legs, as I lost one in a horrific accident involving a banana jam pie at the weekend
Woka
Last edited by Wokawidget; Oct 27th, 2003 at 08:54 AM.
Originally posted by Wokawidget OK, I know where this is happening...it's when you create the ActiveX EXE class in the threadproxy and access 2 of it's properties. This can easily be resolved. I will add this into the new revision.
excellent
I can't force this on my PC as it's a very fast one.
******!
Only have 3 legs, as I lost one in a horrific accident involving a banana jam pie at the weekend
oh no! I think there's a big banana problem right now... I was attacked yesterday by a massive banana toffee sundae , managed to eat my way put of it tho
Hi Woka, its good your code, i think its gonna hep too mucj, hope so, now i'm trying to understand how your code work, it would be great you could attack some kind of documentation, for example, after i be able to uindarstand i have to adapt to the store procedured than i had mentioned before,.
I have atached an image; when i create up to 5 searches the CPU usage get to almost 100%, however, i dont need more than 5 threads in my application, thats good.
I bet u need a fusion powered shuttle to reach my place...
Posts
963
I kept thinking about how to put those stuffs in a DLL, so we can
just create a thread without writing much codes.
Now tinkering with some of my ideas, will post it when it's working
rite!
I'm using apartment threading, but since it's a DLL...well...I'll try
to put SetTimer in use as well, let's see if it works.
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people