|
|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Multithreading
Multithreading - Understanding the pros and cons
One of the greatest problems of the earlier Win16 environment was that an application could do only one thing at one time (that is "single thread"). However with the advent of Windows NT 3.5x, this changed. In 1995, with the release of Windows 95 this ultra powerful technique came to be used in the common PC.
So what is the use of Multithreading ? - Consider Microsoft Word 97 or higher. It checks spelling while you type ! It does it by multithreading - i.e running two "threads" (in layman's language a "thread" is nothing but a piece of code, a sub or function running simultaneously with the main program). In VB 5, a new function AddressOf was introduced that enabled VB programmers to get the address of any public function in a standard module. This enabled developers to use the CreateThread API to create raw Win32 threads. Though this was effective in VB 5.0, with VB 6.0 it crashes miserably !
Even at planet-source-code.com, I came across a multithreading demo using the CreateThread API. Though THE PROGRAM works with VB 6, it is VERY unstable. Also For ... Next loops, Msgbox..., Open .. etc statements do NOT work in the multithreaded procedures!
Does this mean we cannot multithread safely !? Does it mean that we have to worry about
"exception errors" and GPF's popping up any time ?
The Answer is a BIG NO !
Multithreading is VERY easy once you master the concepts... So just have a look at the sample code.. You will understand just how easy it is to perform true and safe multithreading in VB !
If you gained any information, or if this article is useful to you, a vote of yours will be appreciated. If you found it useless.... just DELETE it !
Multithreading In VB 6 - The Safe Way
The trick to effective and safe multithreading in VB is to use the ActiveX EXE project type (set to standalone EXE). The trick here is to create a new object on a new thread by callin the CreateObject() function and to create the Form that you want to be multithreaded from within this object. As a reult the form is created on a new thread, both of them can run almost independently of the other ! The only problem is managing the code re-enterancy - VB calls the Sub main() procedure every time a new object is created - we must find whether the main window is shown or not - if not we must initialize it. This method is actually very easy ! Just check out the sample code and I am sure that you will be Multithreading right
away
What's more - this code now even demonstrates how to communicate between
threads !
And if you found this code useful - be sure to vote for me ! After all, coding is a tough job, and so is writing a tutorial
!
IMPORTANT: You can now download a new generic multithreader component at
the following link http://planet-source-code.com/vb/default.asp?lngCId=26900&lngWId;=1
. This component allows you to multithread any sub or function in a standard
EXE. No ActiveX EXEs needed (PS:Thanks for all your votes And I am happy to know
that my articles are of some use to you !)
| |
Download article
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzipto decompress it.
Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE: 1)Re-scan downloaded files using your personal virus checker before using it. 2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Other 8 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
7/8/2001 11:37:44 AM:Srideep Prasad IMPORTANT MESSAGE FROM AUTHOR:PLEASE
RUN THE DEMO AS A COMPILED EXE ONLY !!!
ALSO MAKE SURE YOU HAVE VB 5(SP2) / VB
6 ONLY !
|
7/16/2001 3:17:15 AM:Edwin Vermeer You probably would like to communicate
between the instances of youre
executable. I did something like this
in an app of mine. The code pasted
below is a rip from this project, so it
probably need some rewriting. If you
want to do this, then create a form
with the name parser. The code in the
main module is also a litle different.
Here it
comes:
'-------------------------------
----
' Put this code in the beginning
of your
app.
'---------------------------------
--
Dim bSubApp as Boolean
If
App.PrevInstance = True Then
' Send
message to master app
dim
txtPrevHandle as String
dim
lngPrevHandle as String
bSubApp =
True
load Parser
DoEvents
txtPrevHandle = GetSetting(App.Title,
"ActiveWindow", "Handle")
lngPrevHandle =
CLng(Val(txtPrevHandle))
Doevents
SendMessage lngPrevHandle, WM_SETTEXT,
0, ByVal CStr("SubApp ") &
Str(Parser!CommandParser.hwnd)
DoEvents
Else
' Open parser form
bSubApp = False
load Parser
DoEvents
End If
|
7/16/2001 3:17:48 AM:Edwin Vermeer '-------------------------------------
' Put the code below in the parser
form
'---------------------------------
---
Dim lngSubApp() as Long
Private
Sub Form_Load()
If not bSubApp
SaveSetting App.Title, "ActiveWindow",
"Handle", Str(CommandParser.hwnd)
Redim lngSubApp(0) as Long
End If
End Sub
Private Sub
CommandParser_Change()
Dim txtCommand
as String
Dim txtSubCommand as
String
txtCommand =
left(CommandParser, instr(1,
CommandParser & " ", " ") - 1)
txtSubCommand = trim(Mid(CommandParser,
instr(1, CommandParser & " ", " ")))
Select Case txtCommand
Case
"SubApp"
' Keep track of all the
sub applications
Redim Preserve
lngSubApp(Ubound(lngSubApp)+1) As
Long
lngSubApp(Ubound(lngSubApp))
= CLng(Val(txtSubCommand))
Case
"Quit"
End
End Select
CommandParser = ""
End Sub
|
7/16/2001 3:19:46 AM:Edwin Vermeer Sorry for the layout. :(
|
7/16/2001 5:02:06 AM:Srideep Prasad Thanks Edwin ! Anyway, I already know
of a method to communicate between
instances. You can use Mailslots for
this, and I have also achieved
inter-thread communication.
|
8/14/2001 7:19:00 AM:Neil Giddings Hold on here m8, I just tested this out
as I too am really struggling with
threads and thought this may work. This
isn't multi-threading properly, if I
put a do loop in the main form to
display the time in the caption with a
doevents in the code to allow me to
click button etc and as part of the
form load in the other form, which
should be multitasked, I put a msgbox
and run the code this is what I
get.
The timer ticks away and
updates as I would expect it too but
then I hit the button to create a new
form the msgbox is displayed and halts
the time being updated. So it isn't
multi tasking at all.
Can you help
me out, as to what's wrong?
|
8/14/2001 12:49:56 PM:Srideep Prasad Neil - The problem you have mentioned
has a simple solution - Check out your
e-mail since I have e-mail the solution
to you
|
8/25/2001 4:21:52 AM:Kristi Good work,got 5 :)
|
8/29/2001 6:07:35 PM:JimM Srideep: How can I get one thread to
terminate another?
|
8/30/2001 1:12:36 PM:Srideep Prasad Jimm, I have e-mail a solution to your
problem.. Please check your mailbox
|
9/6/2001 5:45:17 PM:Tomfoolry Why not post the answers to the
questions here instead of emailing them.
|
9/6/2001 11:06:18 PM:Nazer Mohamed Got 5! Please proceed with posting
answers and try not to send the mail.
|
9/7/2001 7:12:18 AM:Hawkeye Excellent article!! Very timely, I
might add, as I have employed the
techniques in a large database program
I am creating for my client. BTW,
could you send a copy of the answers
you have emailed to the other
commenters, as I have similar
problems.
Thank you and, please keep
bringing this information to our
attention.
|
9/7/2001 1:03:43 PM:Srideep Prasad THANKS FOR ALL YOUR VOTES ! But I would
like to let all of you know that I have
developed a generic multithreader that
you can use to implement multithreading
in any Standard EXE. You could check
out the code at -
http://planet-source-code.com/vb/defau
lt.asp?lngCId=26900&lngWId;=1
|
9/17/2001 6:30:11 AM:Bard Is the most up-to-date code available
to download from Planet-Source-Code.com?
|
9/24/2001 9:48:23 PM:Yannick Srideep: I would also like to know
how
to get one thread to terminate
another?
|
9/24/2001 9:49:38 PM:Yannick Srideep: I would also like to know how
to get one thread to terminate another?
|
11/7/2001 11:26:49 AM:Donald Derrick This is excellent code. It has helped
me get an assignment that aught to have
been given to the C++ coders done. Now
all I need is a timer w/ callbacks and
w/o the need to a hwnd!
|
11/11/2001 3:29:07 AM:sherwin this+works+great%2E++but+it+doesnt+work+
dlls+being+called+by+ASP+and+registered+
under+COM+%2FMTS%2C+please+help%2E
|
7/17/2002 1:58:45 PM:Luis Cantero Thanks a lot for this tutorial, I had
some issues with it and I posted the
solutions I've found for everyone to
profit:
http://www.planet-source-code.c
om/vb/scripts/ShowCode.asp?txtCodeId=369
97&lngWId;=1
|
7/17/2002 3:51:25 PM:Dusp2000 Well, how do you send data from the
mainform to the different threads and
from thread to thread ???
Tnx
|
9/20/2002 10:08:32 AM:TV2k.net I DID IT! I created a working, kinda
fast small HTTP server with this! =)
|
9/28/2002 9:33:40 AM: Good Work Srideep, but I have some
difficulties here.
When I use this
example with Winsock control, either
bind nor listen with UDP / TCP
protocol, I got an error msg. telling
that the 'Addresses is in use', How can
I solve this problem.
Anyway 5
globe from me...
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|