HOME - - / - - / - -
DELPHI COURSE INDEX
- - / - - / - -
- - / - - / - -
Other material for programmers
Delphi Course: First Essay: Events and Multitasking
This page is information rich, and a has search button at the bottom of the page.
Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!
You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!
The lines of sample Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)
Almost all of this course will be taught using lots of hands-on examples. Some things, however, just have to be dealt with the "boring" way. Eat your vegatables. Read the essays. Mother will be pleased. You will progress faster.
You may find typos and rough edges in this. None-the-less, the basic information should be accurate. If something seems wrong, or if you find I've assumed knowledge without explaining it in a previous lesson, please let me know. Please forgive matters of typos, etc. for now. I am not inherently sloppy! The blemishes will be dealt with later.
Delphi is a language for writing programs to run under Windows. If you, like me, are fed up with the Windows monopoly abuse, and the direction Mr.Gates seems to be going, you may be considering making the move to Linux. if you do, you'll find Kylix waiting for you. As I understand it, Kylix can take unmodified (or barely modified) Delphi source-code and produce applications which run under Linux.
Even if you do go to Linux, you will find there, as in Windows, that you are working in a multitasking, event driven environment.
This is Good because you can do clever things relatively easily. This is Bad because you can do nothing as easily as you could do it back in the bad old, limited old, days of mono-tasked systems with no events.
In the old days, a computer would be doing almost nothing apart from what your application specified. This made getting things to work quite easy. It also meant you could only do one thing at a time.
The computer I'm working on at the moment is "doing" six obvious things, and many more under the surface. For example, even as I am typing this, there is an open connection to the internet. That is being guarded by a software firewall. In the lower right of the screen, a clock is displayed, telling me the time. Etc, etc.
I've just ben talking about multi-tasking. I haven't gone into much depth, but you don't need it yet.
More important to you just now is the concept and use of events.
In the applications we've done so far, we've used a button's OnClick event and an edit box's OnChange event.
Think back to the Inches to Centimeters converter application. You didn't write any code in that to say "Watch to see if the user is typing something into the edit box"... and yet, when the user did that, things happened. If you added a "Quit" button to the application, again: you didn't write any "See if the user has the mouse pointer over the quit button, has clicked the mouse" code. All of these things are taken care of for you by Windows (or Linux). The changing of the edit box contents, the clicking of the mouse on the virtual button generate "events". The events travel through the various things which are going on, and, if appropriate, the event handling code in your application is executed whenever the event arises. Pretty cool! A little strange, at first, but if you don't think about it too hard, you'll soon be using it with little difficulty.
If all that's clear, stop reading here.
I guess it wasn't clear?
If you come from the "old school", the following may help.
In the old days, we could write something like:
Get first number
Get second number
Output the sum of the two numbers
Get a key
Until the key was "Q"
Do any shut down things
In those days, the application and the computer would stop and wait patiently at places like "Get a key". In those days, with the program above, you had to give the computer the first number first. You had to supply the second number before you could quit.
A comparable application today would have two edit boxes (for "first number" and "second number". They could be entered in either order. There would be a label in which the sum of the numbers would appear. There would be a button to terminate the application. It could be clicked before any numbers had been entered.
It may help... or may confuse... to think of a Windows computer along the following lines....
Initialise computer, during power on
If an application is requested, start it up
See if event 1 has occured. If so, set flag
See if event 2 has occured. If so, set flag
See if event 3 has occured. If so, set flag
See if event 4, 5, 6 etc... has occured. If so, set flag
For each running application, see if they are interested in
any of the flags which have been set by the
various events. If so, the applications can change their
states, as necessary
Until computer is told to shut itself down
Do any shut down things
It isn't really quite that simple... but that model will suffice for now!
A useful point arises even from such a crude model. A moment ago, I described a possible application, the one with two edit boxes. They would probably each have their own OnChange event handler. In other circumstances, it is useful (and allowed) to have several components all share one bit of code to handle some event.
Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's Sheepdog Software (tm) freeware, shareware page.
Link to Tutorials main page
Link to Delphi Course index
Here is how you can contact this page's author, Tom Boyd.
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....