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

Thread: how to create a new threads in VB??

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50

    how to create a new threads in VB??

    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

  2. #2
    Lively Member
    Join Date
    Aug 2002
    Location
    India
    Posts
    97
    You cannot explicitly create threads in VB.
    Better post your thread in API section
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Dont quote anyone if you can help it -
    THM
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    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:
    1. Option Explicit
    2.  
    3. Dim m As Integer
    4.  
    5. Private Sub Command1_Click()
    6. Dim x As Integer
    7. Dim i As Integer
    8. Dim f As Form
    9. Set f = New Form1
    10. f.Show vbModeless
    11. Do While m <> vbKeyEscape
    12.     If x > 30000 Then x = 0
    13.     Me.Cls
    14.     Print x
    15.     For i = 1 To 9999: DoEvents: Next i
    16.     x = x + 1
    17.     DoEvents
    18. Loop
    19.  
    20. End Sub
    21.  
    22. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    23.     m = KeyCode
    24. End Sub
    25.  
    26.  
    27. Private Sub Form_Unload(Cancel As Integer)
    28.     m = vbKeyEscape
    29.     Unload Me
    30. End Sub

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    This should be more a work of MSVBVM60.dll, rather than your exe itself.

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    hav anybody tried that code!!? any other explanation y VB stops the counter in 1st form when the 3rd from is loaded!??

  7. #7
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    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 .

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    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?

    mine is intel733MHz with XP os..... ur's?

  9. #9
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    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 .

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    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..

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  12. #12

  13. #13
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164
    Let's not fool ourselves...

    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

  14. #14
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  15. #15
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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...

    Woka

  16. #16
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    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.
    Leather Face is comin...


    MCSD

  17. #17

  18. #18
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Have you ever been able to get an activeX exe to run on its own without using a timer of some sort...
    Leather Face is comin...


    MCSD

  19. #19
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  20. #20

  21. #21
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Honeybee!

    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...
    Leather Face is comin...


    MCSD

  22. #22
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    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...
    Leather Face is comin...


    MCSD

  23. #23
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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...

    Woka

  24. #24
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Yes, but the main reason you would, and I have, used an activeX exe is to carry out a long process in the background...

    This can be done simply, without the need for message queus etc...
    Leather Face is comin...


    MCSD

  25. #25
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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...

    Woka

  26. #26
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Under what circumstances would you, and how would you use a message queue?
    Leather Face is comin...


    MCSD

  27. #27

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    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...

  28. #28
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

  29. #29
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Hmm...

    So how would you use a message queue?
    Leather Face is comin...


    MCSD

  30. #30
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  31. #31
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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...

    Woka

  32. #32
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Well ...

    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...

    Woka

  33. #33
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Wokawidget.. WHY DO YOU NEED A MESSAGE QUEUE TO DO THAT???

    All method calls to the activeX exe can be stored within the ACTIVEX exe itself... the timer disabled and reeneabled etc...

    I note that you are still using a timer! ; )

    IT IS IMPOSSIBLE TO DO IT WITHOUT ONE - BE IT API OR STANDARD TIMER CONTROL!
    Leather Face is comin...


    MCSD

  34. #34
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Another point...

    Honeybee, using Create object makes no difference, I just tried it....
    Leather Face is comin...


    MCSD

  35. #35

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50

    Re: Re: Well ...

    Originally posted by Wokawidget
    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.

  36. #36
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

    Woka

  37. #37
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    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.

    http://www.planetsourcecode.com/vb/s...26900&lngWId=1
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  38. #38
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    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!

  39. #39
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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.

    http://www.planetsourcecode.com/vb/s...26900&lngWId=1
    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

    Woka

  40. #40

    Thread Starter
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    Originally posted by Wokawidget
    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......
    Attached Files Attached Files

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