how to create a new threads in VB?? i need to do few task concurrently. such as when VB will load a form to show in the screen, another thread will read the data from a file and load a number of controles at the runtime. the run-time control loading will not halt the form loading. obviously the form load will finish first, so user will still b able to c it while the backgroud thread loads all the rest of the controls
You can use ActiveX EXE application combined with an ActiveX DLL and a Standard EXE to create a multi-threaded setup. Within a single application you can use the CreateThread API function to create threads. However using this API approach is supposed to be quite dangerous.
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
does each form (when VBModeless) r run in a seperate thread?? the answre is NO. nothing in vb seems to run concurrently (bside timer). here's an expriment that i did:
take a new project .. n paste these code in the form1... run it.. each time u click the button it creates a new instance of the form1. since the new form1 loaded as VBModeless it lets the code continue while it loads itself. the old form increment a countr concurrently as it loads the new form1. till now everything is fine... but just look at wat happen if u hit the button of the new form1..... VB stops the 1st form1 n start excute the 2nd form1 while it load the 3nd form1. my guess is, since all the form1 is in a single thread since VB cant manage to run to counter incrementing code at the same time.. but if the story is true then how the hell it mange to load a form while it excuting the code in others!!??!
VB Code:
Option Explicit
Dim m As Integer
Private Sub Command1_Click()
Dim x As Integer
Dim i As Integer
Dim f As Form
Set f = New Form1
f.Show vbModeless
Do While m <> vbKeyEscape
If x > 30000 Then x = 0
Me.Cls
Print x
For i = 1 To 9999: DoEvents: Next i
x = x + 1
DoEvents
Loop
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Originally posted by NAVI_NAVI hav anybody tried that code!!? any other explanation y VB stops the counter in 1st form when the 3rd from is loaded!??
it DOES run at the same time, it just runs really slow. REALLY slow. if you removethe doevents from the LOOP then it will not run when the other is running.
Remember, if someone's post was not helpful, you can always rate their post negatively .
Originally posted by BuggyProgrammer it DOES run at the same time, it just runs really slow. REALLY slow. if you removethe doevents from the LOOP then it will not run when the other is running.
Really!!! it runs?!? well i've been waiting last 5 minute havent seen a lil change!! how slow does it run on ur mechine?? btw wat sort of mechine r u runnin in?
Originally posted by NAVI_NAVI Really!!! it runs?!? well i've been waiting last 5 minute havent seen a lil change!! how slow does it run on ur mechine?? btw wat sort of mechine r u runnin in?
mine is intel733MHz with XP os..... ur's?
P4 1500 XP
Remember, if someone's post was not helpful, you can always rate their post negatively .
mmm double the speed, thats y may it runs (really slow though) in ur mechine. well the bottom line is we will not get same speed concurrent processing form VB anyway..
Originally posted by NAVI_NAVI does each form (when VBModeless) r run in a seperate thread?? the answre is NO. nothing in vb seems to run concurrently (bside timer).
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
I think that multithreading and VB6 are two words that don't come together.
I was experimenting on multitheading (for example reading a file and at the same time putting some information on a listview control) using a free threading model (API declarations --> CreateThread) and my conclusion is that it worked OK on my AMD K6-2 450Mhz, ONLY when using the IDE.
Everytime I compiled to an EXE (all kinds) it CRASHED.
GO FOR VB.NET !! That is the answer. I know we all are used to VB6 to do out projects (more or less) but we HAVE to move to VB.NET sooner or later.
Hyperspaced, did you take the extreme pains of going through all the posts in this thread? In my first post, I have clearly mentioned you need to use ActiveX EXE and ActiveX DLL or simple EXE to implement a multi-threaded setup. An ActiveX EXE is the only good way of implementing multi-threading in VB. And CreateThread is not at all reliable. Everywhere you go for multithreading in VB6.0, you will find that people will discourage you from using the CreateThread API.
The conclusion is that multithreading in VB is definitely possible, but it doesn't have to be through the CreateThread API. You have to use the ActiveX EXE approach for that.
And if you are just not willing to do it, search the web and you will find many ActiveX controls which will help you create a multi-threaded application. I think www.componentone.com has one, also perhaps www.vbaccelerator.com
Before jumping to such conclusions that multithreading and VB don't come (actually "go") together, you should get your facts right
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
I agree. VB and multi threading do work, you just need to know how to do it. Admittedly it's not easy to set it all up, writing ActiveX EXE's and getting them to work in our app. It takes a lot of practice. Definately not something for beginners...I class myself as quite an advanced VB programmer, but even multithreading catches me out sometimes, still haven't got it 100% yet
VB .NET does do it a lot easier and creates proper multithreaded apps with very little effort.
Is it imperitive that your app has to be able to multithread? If not, then don't attempt it...
Originally posted by Leather Running an ActiveX exe on its own thread is quite simple, although it still requires a timer...
The just let the front end wait for events raised by the activex exe.
The use of timers is not at all required in case of an ActiveX EXE and a standard EXE setup. The standard EXE can simply invoke a method on an object within the ActiveX EXE and then proceed with its own processing.
A truly multithreaded setup requires that the standard EXE use an ActiveX DLL which in turn invokes object methods in the ActiveX EXE. True, it's complicated, but building a simple multi-threading example shouldn't be difficult, so as to illustrate that it's still possible. I think the MSDN does have some examples, in the Coffeecup tutorial on building a multithreaded application.
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
An ActiveX DLL is not really needed, as that runs in the same thread as the client, so you could just invoke the ActiveX EXE from your client application.
I agree with what you are saying... HOWEVER i have simply never been able to get this to work, without the use of a timer in the ActiveX exe.
The reason being, in the front end you create an object from the ActiveX exe, you then call a method... At this point the front end exe WAITS for the method to complete.. (If the method returns immediatly then this is fine, but if you need to do some serios stuff in the background then your in trouble).
This occurs irrespective of the method type (Sub or Function).
The only way around this is to have some kind of timer in the ActiveX exe. When the method is called it starts the timer and immediatly returns to the client app. Then the timer event fires and starts the main method, doing all the processing in the background.
If you know another way to do int please share it with me...
Also the example you quote is the MSDN coffe cup example... The problem with this example is that the ActiveX exe uses ! A TIMER ! to simulate events raised by a serial port control. Also this example is not carrying out some processing in the background, it is merely waiting for a timer to fire...
You could get your client to post messages to a message que and get your ActiveX to read from the message que
That's the hard part of multithreading Like we said, it's not simple...
Yea, you can use a timer if you want. For simple background processing this is fine. The problem arises when your ActiveX EXE is a huge part of your project and does many different things.
If a timer works then just stick with that...
Originally posted by Wokawidget
GO FOR VB.NET !! That is the answer. I know we all are used to VB6 to do out projects (more or less) but we HAVE to move to VB.NET sooner or later.. [/B]
well i'll rather mov to VC++ then to go through all VB (vb.net) s#@t again... actually i'm already half way in VC++ but just trying out some of the limits wat VB actually can and cant do... creating a thread in VC++ or C++(unix) is so damn easy but on the other hand VB doesnt giv u any concurrent processing at all!!! if u cant process multipul things at the same time then wats the point of having windows??
creating a thread is not as basic(low level) programming as u guys r thing. its a basic need of multi talk programming. its more then likely u'll com to a point in every project when u gonna need to run 2 or more function concurrently... n thats when threadin coms...
Originally posted by Leather Under what circumstances would you, and how would you use a message queue?
Using the Timer method, invoke your ActiveX EXE to carry out a large background task...It runs in a completely different thread to your client...Agreed? Both now are running independantly...Agreed?
Right, while it's doing the back processing, try and call the same function again
Originally posted by Leather Also the example you quote is the MSDN coffe cup example... The problem with this example is that the ActiveX exe uses ! A TIMER ! to simulate events raised by a serial port control. Also this example is not carrying out some processing in the background, it is merely waiting for a timer to fire...
Use CreateObject() to create an instance of the object from the ActiveX EXE. That should eliminate the need to have a timer.
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
This is the part of MultiThreading that I am just getting into.
What you do is create an API timer in the ActiveX EXE, when you create the object the timer is started and it looks at the message que, if there is nothing for it to act on then it does nothing, but if your client passed a command to the message que for the ActiveX EXE then it starts processing it. During this process you disable the timer. Once it's finish, enable the timer and it looks to see if there is anythign else for it to do, and so on...
Lots of code required to do what would seem a reletively simple process
Once you have the initial code then you can cut and paste it into other apps. Once it's all working together, you can write some very neat and powerfull applications, like Outlook and Word, that can be accessed from other VB projects as well as being true multithreading...
Originally posted by honeybee Use CreateObject() to create an instance of the object from the ActiveX EXE. That should eliminate the need to have a timer.
.
Errrr...how would that help?
If you call a function, which does a lot of background processing, your client app will still hang...
yap u r right. u got a fair idea wat i was been yelling about. bloody need for multi threading. it seems VB6 is really lacking some importent basic stuff.
Originally posted by Wokawidget
Errrr...how would that help?
If you call a function, which does a lot of background processing, your client app will still hang...
not exactly. since the object will b created in a different thread (coz its a activeX exe) so ur host program might not b halt. if the object truely created on different thread u might b able to get concurrent processing. but then again there is a bunch of pain waiting for u if u want activeX exe. u gotto register when u deploy it on a clint computer. it wont b just a simple standerd exe.
Ok, point taken. You are correct in what you say. Now what if your ActiveX EXE is globally referenced, and say has a form that it displays. You don't want hundreds of different instance opening up, you just want one, so you reference it globally, and different parts of your client use different parts of the ActiveX EXE, which you have referenced globaly A timer is not good enough, as it may be doing background stuff, when you call another function from it from a different part of your client...so, now what do you do, since as soon as you try and call a function you get the "Component Pending Function" message box and you have 2 buttons "Switch To..." and "Retry"...that's the last thing you want in your client
www.planetsourcecode.com had an example on threading that worked really well, using Activex. he said that because whenever it went through, msvbvm60.dll, createthread would screw up, so he had to find anther way around it.
At the outset, it looks like using a timer is the only way of making a procedure call return from the ActiveX EXE immediately so the client is not blocked. However this may not be the only way. Perhaps, as Dan Appleman says, we really need all the three types of components, i.e. a standard EXE, an ActiveX DLL and an ActiveX EXE together to implement a truly multi-threaded setup.
Secondly, we have not used any objects in the client EXE, have we? Perhaps using objects and interfaces it may be possible to eliminate the need of a timer, where the client object raises an event which is trapped by the server, and the server object raises an event that is trapped by the client ...
.
I am not a complete idiot. Some parts are still missing. Check out the rtf-help tutorial General VB Faq Thread Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink! Get more power for your floppy disks. ; View honeybee's Elite Club: Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
And if that response was mine, please think about giving me a rep. I like to collect them!
Originally posted by BuggyProgrammer www.planetsourcecode.com had an example on threading that worked really well, using Activex. he said that because whenever it went through, msvbvm60.dll, createthread would screw up, so he had to find anther way around it.
Tried that example, but it killed my processor speed, and the app virtually stopped responding, well slow That raised a Terminate error and shut down my VB Booooooooooooooooooooo.
Won the programmer award, so it must be pretty good, although the award is only given due to the amount of other programmers who vote for it, so it may not always be correct code
here, try this one.... it'll work coz i just wrote n tested it.
it uses ActiveX exe to create threads. no API or Timer is used.
Bfore u start: open the Group1.vbg and compine Project2 to Project2.exe then run project1.
once u run project one it'll show u a form. click the thread create button. i'll create 3 threads representing 3 form on the screen. each of those form will hav a button called 'do Something' click it, it'll start a counter but this time all three counter will b able to run at same time..... concurrently at last...... wow........damn......