Board Logo
« SCAN sample program on the Wiki »

Welcome Guest. Please Login or Register.
Sep 25th, 2017, 7:06pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: SCAN sample program on the Wiki  (Read 4518 times)
tooanalytical
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1739
xx SCAN sample program on the Wiki
« Thread started on: Aug 20th, 2010, 9:44pm »

This is really directed to Cundo, who put up the BB_... SCAN example demonstration program on his Wiki.

Cundo, that is a fairly understandable example program. I find SCAN to be difficult to understand and use. Could you create other very simple demonstration example programs to show how SCAN is used? Do you have some guidelines for when we should use SCAN instead of WAIT ? Do you have guidelines for when we should use WAIT instead of SCAN ?
User IP Logged

Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: SCAN sample program on the Wiki
« Reply #1 on: Aug 21st, 2010, 03:44am »

Uggggg.... I had a fiddle with the Wiki and didn't do to well. Perhaps Cundo could sort out the formating or just revert to the original text.

When you use Scan and Wait is pretty simple. If you have nothing to do you Wait, if you are in the middle of something you Scan. Much like real life really.
« Last Edit: Aug 21st, 2010, 03:45am by Rod » User IP Logged

uncleBen
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1679
xx Re: SCAN sample program on the Wiki
« Reply #2 on: Aug 21st, 2010, 04:36am »

The explanation seems a bit off, though.

SCAN, as far as I can see, does not insert any pause in the program no more than you could call "LET var = 10" a very short pause that assigns 10 to var :).

The messages that can be processed, should be any messages JB programs can handle (including TIMER).

As to its usage in loops, if you don't carefully make sure that after the event is handled you execution resumes within the same loop, SCAN could be another illegal way to goto out of a loop.

(This is regarding the original version, now it is much better.)

--------------------------

What goes on is something like the following:

When you press a button, type a key, click with the mouse, or when the TIMER's time is up, etc, Just BASIC naturally won't branch to the handler immediately because your code might be in the middle of something.

Instead information about the events is nicely queued up, so JB can make your program jump to the event handler when your code indicates that it is ready to do so.

Most of the time, the queue is empty - all arriving messages have been handled. Sometimes the queue can fill up, for example, when a program has a loop taking much time, giving the user plenty of time to press various buttons, move the mouse around etc.

To indicate that you want to handle events at a spot in the code, you can use either the SCAN or WAIT command.

When there are already messages queued up in the list, SCAN and WAIT do the same thing: grab the first event from the list and branch execution to the event handler of this event (removing the event from the job list).

Suppose the message queue looks like this (what might have been going on while your program was spinning in a long loop):

Code:
------------------------------------------------------------------
| MouseMove    | MouseSingleCl |  Timer         | ButtonClick   |
|  #main.g     |    #main.g    |                |   #main.ok    |
| 45  102      |   48  104     |                |               |
|-----------------------------------------------------------------
 


Both SCAN and WAIT would check and see that the oldest event not handled this far is a MouseMove event. So your program immediately branches to whatever branch label you have registered as the handler for MouseMove events.

The event queue may also be empty. Normally a program can process input faster than humans can give it.

Code:
------------------------------------------------------------------
|              |               |                |               |
|              |               |                |               |
|              |               |                |               |
|-----------------------------------------------------------------
 


This is where WAIT and SCAN are different.

When you use SCAN to check the event queue and it is empty, it "returns" immediately and the next line of your code will be executed.

When you use WAIT, your program will not continue until an event happens, after which execution is branched to the handler.

----------------

As to practical usage of SCAN, yes, it would be useful within some loop.

For example, sometimes a task can take very long (such as searching recursively for a particular file on the computer), and you may want to provide means for the user to abort the search.

The help file example also shows such a loop (even though you could rather give the computer some rest and use the TIMER to create a timed loop).

Here's another example of a task that might take very long with some input (e.g try to determine if 100000000000097 is a prime):

Code:
    nomainwin

    WindowWidth = 312
    WindowHeight = 180

    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)

    statictext #main, "Input a number to test if it is a prime:", 26, 16, 264, 20
    textbox #main.numberField, 54, 51, 144, 25
    button #main.go, "Go", [testPrime], UL, 30, 96, 96, 25
    button #main.cancel, "Cancel", [cancelTest], UL, 134, 96, 96, 25
    open "Prime Checker" for window as #main
    #main, "trapclose [quit.main]"
    #main, "font ms_sans_serif 10"
    call EnableGoButton
    wait

[quit.main]
    Close #main
    END

[testPrime]
    #main.go, "!disable"
    #main.cancel, "!enable"
    #main.numberField, "!contents? value"
    value = int(value)
    ValueIsPrime = 1
    if (value > 2 and value mod 2 = 0) or value < 2 then
        ValueIsPrime = 0
    else
        if value > 2 then
            maxValueToTest = int(sqr(value))
            i = 3
[primeTestLoop]
            if i > maxValueToTest then goto [TestComplete]
                if value mod i = 0 then
                    ValueIsPrime = 0
                    goto [TestComplete]
                end if
                'This here allows us to quit the loop if user gets impatient
                scan
            i = i + 2
            goto [primeTestLoop]
        end if
    end if
    'Output results
[TestComplete]
    if ValueIsPrime then
        notice str$(value) + " is prime"
    else
        notice str$(value) + " is not prime"
    end if
    call EnableGoButton
    wait

[cancelTest]
    notice "Prime test aborted"
    call EnableGoButton
    wait

sub EnableGoButton
    #main.go, "!enable"
    #main.cancel, "!disable"
end sub
 
« Last Edit: Aug 21st, 2010, 04:38am by uncleBen » User IP Logged

Passing arrays to subroutines, functions that work with any types, quick string indexing and much more - JBExtensions.

Tired of Minesweeper? Try TomatoSweeper
jaba
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1049
xx Re: SCAN sample program on the Wiki
« Reply #3 on: Aug 21st, 2010, 09:41am »

on Aug 21st, 2010, 03:44am, Rod wrote:
Uggggg.... I had a fiddle with the Wiki and didn't do to well. Perhaps Cundo could sort out the formating or just revert to the original text.

Rod;
This is a pretty easy fix, and you'll probably get a kick out of doing it yourself. So, here goes...

Click the "Edit this page" button. On the right end of the editor toolbar, click the arrow next to the "Save" button and select "Wikitext Editor. You should see the listing of your page with all the wikitext mark-ups. Here's the part with the Example code:
Code:
==Example== 
[[code format="vbnet"]]
<span style="height: 315px; width: 767px;"> </span>
[ [code]]
' If you remove the Scan statement from the loop
' the only way to quit the program is with the Ctrl+Break combo

button #1.b, "End", [quit], LR, 50, 10
open "Scan Test" for window as #1
#1 "trapclose [quit]"



[start]
scan
a=a+a
goto [start]

[quit]
close #1
end


==Useful Functions== 

The <span> tags appear to be unnecessary, so remove that line - or move it up to just under the ==Example== heading. Move the "code" tag to after the end statement in your code example to close the code "formatting". Click Preview to be sure its good then "Save", and you're done.

Listing after changes:
Code:
==Example== 
<span style="height: 315px; width: 767px;"> </span>
[[code format="vbnet"]]

' If you remove the Scan statement from the loop
' the only way to quit the program is with the Ctrl+Break combo

button #1.b, "End", [quit], LR, 50, 10
open "Scan Test" for window as #1
#1 "trapclose [quit]"



[start]
scan
a=a+a
goto [start]

[quit]
close #1
end
[ [code]]    'no space between [ [

==Useful Functions== 
User IP Logged

JACK - Windows 8.1 64-bit; 2.5 GHz Intel i3 processor; 6.00 GB RAM;
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: SCAN sample program on the Wiki
« Reply #4 on: Aug 21st, 2010, 11:00am »

Right, put it back into order. I just wonder how we keep both beginners and more aware coders happy. Perhaps we need an initial paragraph that targets beginners. Then the more subtle things can be brought out.
User IP Logged

JohnDavidson
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 4142
xx Re: SCAN sample program on the Wiki
« Reply #5 on: Aug 21st, 2010, 11:17am »

Quote:
Perhaps we need an initial paragraph that targets beginners.

Not a paragraph but simply put, Scan checks for user input and Wait waits for user input.

John
User IP Logged

John Davidson
e-me: johnshomeport@yahoo.com
My JB Page: http://john.jbusers.com/
Did ya Libby yet? http://lblibby.com/
Facundo
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1289
xx Re: SCAN sample program on the Wiki
« Reply #6 on: Aug 21st, 2010, 6:36pm »

That is a much better explanation than mine, thanks Rod for taking the time to edit the page.


About the SCAN being a pause, I read the help file and it says "stop what it is doing for a moment" that is why I called 'stop for a moment'=pause. So many thanks for the explanation regarding the message queue uncleBen.


I use SCAN when debugging, to easily quit from loops, especially loops whose are printing data to the MAINWIN.


Quote:
Scan checks for user input and Wait waits for user input.
I like that, but how would it be using a synonymous for 'waits' ?

User IP Logged

cundo aka MSlayer
TyCamden
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1430
xx Re: SCAN sample program on the Wiki
« Reply #7 on: Aug 21st, 2010, 9:31pm »

on Aug 21st, 2010, 11:17am, JohnDavidson wrote:
Not a paragraph but simply put, Scan checks for user input and Wait waits for user input.


How about...

Scan checks for user input briefly before moving on in the program, and Wait pauses the program indefinately to await user input.
User IP Logged

TyCamden

Please give credit if you use code I post, no need to ask for permission.


Just BASIC 1.01, Windows 7 Home Premium version (2009), AMD Athelon II 320 Dual-Core Processor 2.10 GHz - 4.00 GB RAM (3.75 usable) - 64-bit OS
jaba
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1049
xx Re: SCAN sample program on the Wiki
« Reply #8 on: Aug 22nd, 2010, 12:03am »

Until I read uncleben's explanation of scan and wait, I had an entirely different idea of how scan worked. Because its called "scan" I always thought of it as constantly scanning for events whenever the branch containing it became active. I could never "imagine" how that would actually work. Now, I think I understand that scan does nothing until the code reaches the scan statement and then it makes a quick trip to check for events and returns back to the next line of code - (just as uncleben explained it). I wonder how many other beginners might have the same concept?

I wondered how the timer statement worked in this regard. So, I ran the example from the help file and found a couple of things. Here's the code:
Code:
    'set a timer to fire in 3 seconds
    'using branch label event handler
    timer 3000, [itHappened]
    'wait here
    wait

[itHappened]
    'deactivate the timer
    timer 0
    confirm "It happened!  Do it again?"; answer
    if answer then
        'reactivate the timer
        timer 3000, [itHappened]
        wait
    end if
    end
 

I found that if I press the close button ("X" in top right corner) while the timer is "counting", the "terminate" confirm window comes up. If I click the yes button, the program ends properly. However, if I do the same thing and do NOT respond to the confirm dialog, and the timer fires and brings up the "Do it again?" dialog, then click yes to the terminate dialog, the program will crash. That's not good!

So, it looks to me as though the timer has a sort of built-in scan service, for one thing. Second, it looks like that feature does not work right.

User IP Logged

JACK - Windows 8.1 64-bit; 2.5 GHz Intel i3 processor; 6.00 GB RAM;
uncleBen
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1679
xx Re: SCAN sample program on the Wiki
« Reply #9 on: Aug 22nd, 2010, 03:34am »

There is nothing magical in the timer either. A TIMER command only means, that in x milliseconds, a special event will be generated.

It is as if you told someone to keep an eye on the stopwatch and tell when the time is up. (And then not turn off the stopwatch, but keep notifying you periodically.)

-----------

As to the problem, it is probably that the WAIT command was not intended to be used in a non-GUI program.

JB could be a bit more stable, though, when it comes to TIMER.

Personally I think that the JB Runtime should just automatically turn off the timer if a modal dialog comes up (be it a NOTICE or FILEDIALOG, or a JB error dialog or terminate prompt etc).

« Last Edit: Aug 22nd, 2010, 03:39am by uncleBen » User IP Logged

Passing arrays to subroutines, functions that work with any types, quick string indexing and much more - JBExtensions.

Tired of Minesweeper? Try TomatoSweeper
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3553
xx Re: SCAN sample program on the Wiki
« Reply #10 on: Aug 22nd, 2010, 1:16pm »

The page now says:
Quote:
The SCAN statement is used to check for events like key presses, mouse clicks or expired timer events. It is rather like an INPUT statement but it does not stop or wait.


I fail to see *ANY* likeness with INPUT.
Could someone enlighten me?

If no, it probably should be corrected.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: SCAN sample program on the Wiki
« Reply #11 on: Aug 22nd, 2010, 2:34pm »

It was in the original text and scan does cause input to be read and reacted to. But any better words are most welcome.

User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3553
xx Re: SCAN sample program on the Wiki
« Reply #12 on: Aug 23rd, 2010, 01:17am »

I deleted "It is rather like an INPUT statement but it does not stop or wait. "
Part "It is rather like an INPUT statement" is just wrong, and part " but it does not stop or wait. " repeated in next sentence (" If not [ any event has fired ] program continues without stopping. ")

EDIT:
Quote:
It was in the original text

? the help file ? Well, shouldn't we do better if we can? wink
« Last Edit: Aug 23rd, 2010, 01:18am by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls